Category Archives: IIS

How to Programmatically Retrieve The Physical Path from an IIS Site

While looking for a way to automate some IIS tasks, I discovered this, Microsoft Produced, NuGet package. It appears to give almost full control over IIS to your code. I’ll try and post more about its features and limitations in further posts.

The Microsoft NuGet package Microsoft.Web.Administration:

NugetWebAdmin

The following code will trawl all the sites hosted on the local machine, and print some key information about each application within, including the physical path.

            ServerManager sm = new ServerManager();
            foreach (var s in sm.Sites)
            {
                Console.WriteLine($"Name: {s.Name}, state: {s.State}");
                
                foreach(var app in s.Applications)
                {
                    Console.WriteLine($"\t{app.ToString()}, path: {app.Path}");

                    foreach(var vd in app.VirtualDirectories)
                    {
                        Console.WriteLine($"\t\tPhysical path: {vd.PhysicalPath}");
                    }
                }
            }

Output:

WebAdminOutput