Share via


How to Get IIS Web Sites Information Programmatically

I needed to get the ___location of IIS log files on my servers, after doing a quick investigation, I am quite amazed on how much information are exposed via managed code.

This snippet will return the name of the sites and the ___location of the log files.

 foreach (DirectoryEntry Site in new DirectoryEntry("IIS://" + System.Environment.MachineName + "/w3svc").Children)
    if (String.Compare(Site.SchemaClassName, "IIsWebServer", StringComparison.OrdinalIgnoreCase) == 0)
        Console.WriteLine(Site.Properties["ServerComment"].Value.ToString() + " == " + Site.Properties["LogFileDirectory"].Value.ToString());

To get more information about what fields and method you can access, please refer to this MSDN doc: https://msdn.microsoft.com/en-us/library/ms524487.aspx.

Just remember, in Vista/Windows Server 2008, you will need to run that code with elevated privilege.