要获得字符串的文件名 [英] To get the file name in string

查看:161
本文介绍了要获得字符串的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过以下code存储JSON文件在我的项目文件夹外,

  System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath(JSON.json),jSearializer.Serialize(modified_listofstrings));

但现在另一种方法,我想读该文件的唯一的名字,我是多么想做到这一点,

如果我读了字符串文件,我得到的文件的内容,但我只是想通过它存储的文件的名称。

任何帮助将大大AP preciated。

---编辑提个问题 - 包括code的文件名。

 字符串文件名= ID.Replace(_。);
System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath(文件名+。JSON),jSearializer.Serialize(modified_listofstrings));


解决方案

一个解决办法是在 System.IO 命名空间中的类。

有这样做的各种方法。下面我用的MapPath 采取的虚拟目录,我搜索与以.json结尾的文件。这应该让你的文件名

 路径字符串= System.Web.HttpContext.Current.Server.MapPath(/);
字符串[] =文件Directory.GetFiles(路径,*以.json);

I have stored JSON file in my project folder externally by the following code,

 System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("JSON.json"), jSearializer.Serialize(modified_listofstrings));

but now in another method I want to read only name of that file, how I am suppose to do that,

if I read the file in string, I get the content of the file but I just want the name of the file by which it is stored.

Any help will be greatly appreciated.

--- Edited question--- includes the code for filename.

string filename = ID.Replace(".", "_");
System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath(filename+".json"), jSearializer.Serialize(modified_listofstrings));

解决方案

One solution would be to use the classes in System.IO namespace.

There are various ways of doing it. Below i have taken the virtual directory using MapPath and i search for files ending with '.json'. This should get you the file names

string path =  System.Web.HttpContext.Current.Server.MapPath("/");
string[] files = Directory.GetFiles(path, "*.json");

这篇关于要获得字符串的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆