To get the file name in string

谁都会走 提交于 2019-12-11 21:31:36

问题


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));

回答1:


You can save ID in user's session, and in another method access it:

Session["FileName"] = filename;

and in your other method:

string fileName = Session["FileName"];

You can access session via:

System.Web.HttpContext.Current.Session



回答2:


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");


来源:https://stackoverflow.com/questions/19995251/to-get-the-file-name-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!