[ASP.NET]ASP.NET and physical path

In ASP.NET page, the following code:

 Directory.GetCurrentDirectory();

will return "c:\windows\system32\inetsrv" this means, if you writing file without specify a directory, this file will be writen to "c:\windows\system32\inetsrv". for example, in asp.net the following code will write a test.txt to this system folder.

 System.IO.StreamWriter file = new StreamWriter("test.txt");
 file.WriteLine("good");
 file.Close();

Sometimes, we want to write file to a specified virtual folder, say, App_Data folder. how can we get the physical path of this App_Data file? we can use the following code to get the physical path of current virtual folder.

 Server.MapPath("~/");

Then, by

 Server.MapPath("~/App_Data") 

We have the physical path of App_Data folder.

If it is non-asp.net environment, then use
   string path = HostingEnvironment.MapPath("~");

 

blog comments powered by Disqus