Click here to Skip to main content
15,126,317 members
Articles / Web Development / IIS
Posted 27 Nov 2008


55 bookmarked

Programmatically Manage IIS

Rate me:
Please Sign up or sign in to vote.
4.48/5 (12 votes)
27 Nov 2008CPOL5 min read
Base library for programmatically managing IIS through C# and Directory Services.


This article is about managing IIS programmatically. It provides a class library that represents the tree hierarchy used by IIS for its every aspect. The project is accompanied by a Windows Forms applications that is used to pinpoint and extract any data required.


For the company I work, we have several applications of which two are N-Tier, requiring a Smart Client, an application server, and single or more databases. The deployment of our product to an existing client is difficult, and requires database schema upgrade and website restart. Additionally, creating a new environment needs to be parameterized. So, in order to create a Deploy and Environment Application, an IIS management know-how was required.

After searching online, I found some methods, but none that could give an object oriented approach to the problem. Also, there are few knowledge base articles, especially from Microsoft, about this stuff. For example, the version of IIS in a query could not easily be found, and whenever it was mentioned, it required a Registry or file query, which is not helpful when managing IIS remotely.

Explaining the library

The class library is DirectoryManager. It was named like that because having dealt with Active Directory in the past, I came to realize that IIS is a single aspect of the same stuff with different data.

The understanding of the libray requires very good understanding of System.DirectoryServices. The entry point for each node in the IIS hierarchy is the base class Base.BaseEntry, which is a wrapper for a DirectoryEntry and provides an additional functionality. The most important is a wrapper to the Properties collection found in the directory entry. Some additional methods are provided like commit changes. In any case, the instance of the DirectoryEntry and its children are exported via the SourceEntry and Children properties, respectively.

The properties are represented with the classes PropertyValue and PropertyValues. The primary reason is that the property value system of directory entry required a constant knowledge of the type of data and casting, making the code difficult to support and understand.

Sarafian.Framework.General.DirectoryManager.IIS contains the classes that represent the IIS hierarchy, at least whatever was required for me. Every node of this hierarchy finds its corresponding sub-nodes, but also knows about its parent. Everything is typed, and if anything is missing, it can easily be added. In many cases, special care has been taken for version incompatibilities between versions 6 and 7 that have been tested. Every class node provides its member functions that add a subnode. For instance, the Service class which represents the IIS service provides functionality to add and query websites. The WebSite class provides functionality to start, stop, restart, and of course, check the status.

Here is what every class represents in the hierarchy.


It represents the IIS Service. It is the entry point for the IIS management system. The service holds the reference to single or multi AppPools, Schema, WebSite and Info classes.

  • AppPools holds the list of AppPool classes that logically describe the application pool feature of IIS
  • Schema holds the AppIsIsolated reference that, among others, knows whether the IIS service is under an NT system.
  • Info has the version info of the service. They are provided by MajorIISVersionNumber and MinorIISVersionNumber.
  • WebSite is the actual website. Each website on IIS has a SiteID that really tells nothing about it, but is required to query and find the website. I can't understand why, but the actual physical path is held within a WebVirtualDir which is almost identical to VirtualDirectory. Here are some important methods. ConvertToApplication makes a virtual directory an application for IIS and not just a simple folder, and ApplyASPNETV2 applies Asp.Net2 to its application pool.
  • WebVirtualDir is practically the root of the website.
  • VirtualDirectory represents a virtual directory on a website.

Both WebVirtualDir and VirtualDirectory have a PhysicalPath property.

Some of the properties of child nodes are propagated to the owner node, because it seemed to me that they should be placed there. For example, WebSite has a PhysicalPath property, but it is actually a reference to PhysicalPath in WebVirtualDir.

Using the code

In the IISTreeView, the service is created like this:

this.service = new Service("LocalHost");

The IIS hierarchy is represented with a tree, and when selecting on a node on the tree, a dataset with its properties is produced and shown on the right. Because all nodes of the IIS hierarchy have not been created in a typed class, a DirectoryEntryNode exists that handles a simple DirectoryEntry. Basically, this project could help you query anything that can be accessed through System.DirectoryServices.

If a new property is required to be typed, then you create it with the same name and corresponding type. Extra functionality to parse, for example, data from the IIS must be implemented. For example, Port on the WebSite.

public int Port
      return Convert.ToInt32(ServerBindings.Replace(":", "")); 
    catch (Exception)
    return -1;
  set { ServerBindings = String.Concat(":", value.ToString(), ":"); }

Points of interest

The state of the WebSite has a problem, and requires a new DirectoryEntry to be created for each call. So, this is used:

public WebSiteStates State
        DirectoryEntry site = new DirectoryEntry(base.SourceEntry.Path);
        return (WebSiteStates)Convert.ToInt32(site.Properties["ServerState"].Value);

Whenever a tree node requires to show all of its children in the tree, just add this in the constructor:


The above structure may require more code, but it gives a typed and so much more meaningful representation of what happens in any Directory Service of Microsoft. That includes the Active Directory Service and, as far as I know, Microsoft Exchange. Microsoft is very secretive about the way things are stored in these services, so the Tree and Grid approach provides a helpful insight of the service which can be user to create the appropriate classes, and gradually build or extend a library.


This is version 1 of the library. It currently provides types functionality for IIS only.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Greece Greece
I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies

I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.

I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.

I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

Comments and Discussions

QuestionUpgaded solution to VS2012 and got an error Pin
Member 100617782-Aug-13 12:00
MemberMember 100617782-Aug-13 12:00 
AnswerRe: Upgaded solution to VS2012 and got an error Pin
aSarafian2-Aug-13 12:45
MemberaSarafian2-Aug-13 12:45 
QuestionConnect to Rremote Server Pin
manorithan5-Dec-11 22:48
Membermanorithan5-Dec-11 22:48 
AnswerRe: Connect to Rremote Server Pin
aSarafian6-Dec-11 7:27
MemberaSarafian6-Dec-11 7:27 
GeneralI hope you can modify it !! Pin
JLKEngine00825-Dec-08 23:38
MemberJLKEngine00825-Dec-08 23:38 
GeneralRe: I hope you can modify it !! Pin
aSarafian26-Dec-08 2:19
MemberaSarafian26-Dec-08 2:19 
GeneralDelay and bug Pin
aSarafian3-Dec-08 2:55
MemberaSarafian3-Dec-08 2:55 
GeneralRe: Delay and bug Pin
Marcel Wijnands19-Dec-08 1:35
professionalMarcel Wijnands19-Dec-08 1:35 
GeneralRe: Delay and bug Pin
aSarafian19-Dec-08 2:47
MemberaSarafian19-Dec-08 2:47 
GeneralThis looks interesting! Pin
hughd2-Dec-08 2:07
Memberhughd2-Dec-08 2:07 
GeneralRe: This looks interesting! Pin
L Hills3-Dec-08 0:57
MemberL Hills3-Dec-08 0:57 
GeneralRe: This looks interesting! Pin
aSarafian3-Dec-08 2:49
MemberaSarafian3-Dec-08 2:49 
GeneralRe: This looks interesting! Pin
aSarafian3-Dec-08 2:53
MemberaSarafian3-Dec-08 2:53 
GeneralExcellent Pin
merlin98128-Nov-08 5:08
professionalmerlin98128-Nov-08 5:08 
GeneralRe: Excellent Pin
aSarafian3-Dec-08 2:49
MemberaSarafian3-Dec-08 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.