How to Get a Web Application’s Directory Path in ASP.NET

| February 21, 2009 | 1 Comments

I found this question asked a lot in forums so I thought I should answer it here. Getting the physical path of the website can be done using Request.PhysicalApplicationPath. The following code also lists the sub directories of the website and how many files each one has.

string websitePath = Request.PhysicalApplicationPath;

System.IO.DirectoryInfo serverDir = new System.IO.DirectoryInfo(websitePath);
System.IO.DirectoryInfo[] subDirectories = serverDir.GetDirectories();

Label1.Text = websitePath;
foreach (System.IO.DirectoryInfo subDirectory in subDirectories)
{
    Label1.Text += "" + subDirectory.Name + ": " +
        subDirectory.GetFiles().Count().ToString() + " Files";
}

Related Posts:

Filed Under: ASP.NET

Follow us on Twitter

Comments

  1. a says:

    get file path in asp.net 2.0

Leave a Reply