问题
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