How to Programmatically Retrieve The Physical Path from an IIS Site

February 26, 2016

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



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024