IIS Home @ it-notebook.org

Enumerate enabled modules for a specific website in C#

(Kristofer Gafvert, July 6, 2010)

This example shows you how to enumerate the enabled modules for a choosen website. It uses the Microsoft.Web.Administration class, which provides all the necessary classes for configuring and administrating IIS 7.

For additional information, see comments inline, and the System.Web.Administration reference (link at the bottom).

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;
 
namespace IT_notebook.IIS
{
    class ListModules
    {
        static void Main(string[] args)
        {
            string website = args[0]; //Get the website name from an argument
            
            ServerManager serverManager = new ServerManager();
            
            SiteCollection sitesOnServer = serverManager.Sites; //Get a list of websites on the server
            
            bool siteFound = false;
            //iterate all websites on the server, and see if we can find the one we are looking for
            foreach(ConfigurationElement element in sitesOnServer.GetCollection())
            {
                if(website == element.GetAttributeValue("name").ToString())
                {
                    //We have a matching website
                    siteFound = true;
                    break;
                }
                //Console.WriteLine(element.GetAttributeValue("name"));
            }
            
            if(siteFound)
            {
                //Get the configuration for the website
                Configuration config = serverManager.GetWebConfiguration(website);
                //Get the configuration section for modules
                ConfigurationSection section = config.GetSection("system.webServer/modules");
                foreach(ConfigurationElement element in section.GetCollection())
                {
                    Console.WriteLine(element.GetAttribute("name").Value);
                }
            }
            else
            {
                string response =     "Sorry, but there is no matching website. " + 
                                    "Make sure you type the exact name, as appeared" +
                                    "in IIS Manager";
                
                Console.WriteLine(response); 
            }
        }
    }
}

To compile from the command line, run the following on a default installation of Windows Server 2008:

C:\Windows\Microsoft.NET\Framework\v2.0.50727>csc /reference:"C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll"
 /target:exe /out:"c:\DOCUMENTS\listModules.exe" c:\DOCUMENTS\listModules.cs

You need to run the application as a user with privilegies to access the configuration files.

Running the example C# application enumerating modules for a website

Applies to [?]

IIS 7.0, IIS 7.5

Resources

Download source and runnable application
Microsoft.Web.Administration Namespace
IIS 7 Modules Overview