Use C# to manage IIS

Copyright

IISManager is a product of luckzj. Anyone could use this product for free but without commercial purpose. If there’s any problem using IISManager, feel free to contact me at luckzj12@163.com. Or visit my website http://soft-bin.com.

Introduction

We can use IIS management tools provided by Microsoft to manage our IIS server. However, sometimes we need to manipulate our IIS server inside our application. For example, to deploy our website related applications. I will give a brief on how to manage IIS server using C#. Along with that, I will also provide an IIS management DLL called IISManager and its source code writing in C# language.

The target version of IIS discussed in this article is IIS6.0 or higher. Version of IIS on Windows Xp is IIS 5.1 which cannot meet this requirement. However, you can also use IISManager to manage it under the condition that you don't do operations unsupported by IIS 5.1.

IISManager can only provide website server management classes for the moment. However, I would continue to work with it and make it a perfect tool to manipulate IIS servers.

I will start this article with Active Directory Service.

Active Directory Service

I will use Active Directory Service (ADS) to manipulate IIS server. No further discussion would be made about ADS for there’s already too many introductions on the web. I would only discuss how to use it here.

To get access to Active Directory Service, we would use classes under the namespace System.DirectoryServices which is defined in .NET assembly System.DirectoryServices. So we need to add this assembly to our project.

The most important class for IISManager in System.DirectoryServices is DirectoryEntry. We can take DirectoryEntry as a node which contains a set of properties, methods and child nodes. We can use DirectoryEntry.Properties[key] to access properties and use DirectoryEntry.Invoke to access methods of the node. We can get the prototype name of the node with DirectoryEntry.SchemaClassName.

The constructor of DirectoryEntry accepts one parameter which represents an ADS node. I will use an IIS ADS node to construct a DirectoryEntry instance and use it to access IIS servers.

Open IIS website with DirectoryEntry

We can use the following way to get a DirectoryEntry instance to access IIS Active DirectoryServices (ADS):

I use IISWebsite to represent a IIS website and use static method ISWebsite.OpenWebsite() to get an instance of this class.

DirectoryEntry.SchemaClassName means the prototype of the node. The prototype name of IISWebServer is IISWebServer. We would find the website with the specified name. However, DirectoryEntry.Name is not the name of the website, but the name of the node. To get the name of the website, we should use DirectoryEntry.Properties["ServerComment"]. Each property in DirectoryEntry.Properties is represented as a collection, we should use foreach to access members of a property. However, we know that a website must have a name, so we use Server.Properties["ServerComment"][0] to get the real name of this website.

Create a Website

To create a website, we need to add a new node to “IIS://localhost/W3SVC”. I will list the source code and give the explanation later.

It has already been clarified that the name of the node does not mean the name of the website. Actually, for a website node, name of the node is an integer. So when I'm trying to create a new website node, I should first find out the largest integer that exists and add 1 to it which makes our new website node name.

Then, we should set properties for this website including website name and port.

Website name:

node.Properties["ServerComment"].Add(name)

Server port:

The property name for server port is “ServerBindings”. However, we should add “:” to the real port at the front and rear of the port string. “:8080:” for example. I don't know why Microsoft did this, I just do what they told me to do.

So far, we created a website. However, a website must have a root directory:

A website root is a child node of the website named “ROOT” and with a SchemaClassName of IIsWebVirtualDir. I also created a class IISWebVirtualDir to manipulate virtual directories. I will discuss it later. All we should know here is that we created a root virtual directory for our website.

Virtual Directory of a Website

Every website has virtual directories, at least a root virtual directory as discussed above. The other virtual directories should be sub nodes of the root virtual directory. I created the class IISWebVirtualDir to represent virtual directories.

To access the root directory of a website, we can use IISWebsite.Root:

Comments and Discussions

How can i restart my website that i have created using this c# program?We can open inetmgr and then after selecting website , we can stop and start or restart website in manage section in the right hand side.So, I want to do the same through c# code.

I have a directory in my default web site(like C:\inetpub\wwwroot\MyDir). , I want to set this directory to application. But when I use the CreateSubVirtualDir function, it will result in an files have already exist exception.How can I do?

I think you need to have IIS version 6 or older installed on the machine you're running it on (so you can't run it on XP Home for instance). On Windows Server 2008, you need the IIS 6 compatibility kit - IIS 7 uses a different method of accessing its data.

MD_ACCESS_READA value of true indicates that the file or the contents of the folder may be read through Microsoft Internet Explorer.

--------------

Bitmask(hex 0x00000002)

MD_ACCESS_WRITEA value of true indicates that users are allowed to upload files and their associated properties to the enabled directory on your server or to change content in a Write-enabled file. Write can be implemented only with a browser that supports the PUT feature of the HTTP 1.1 protocol standard.

------------

Bitmask16 (hex 0x00000010)

MD_ACCESS_SOURCEA value of true indicates that users are allowed to access source code if either Read or Write permissions are set. Source code includes scripts in Microsoft ® Active Server Pages (ASP) applications

-------------

Bitmask1024 (hex 0x00000400)

MD_ACCESS_NO_REMOTE_WRITEA value of true indicates that remote requests to create or change files are denied; only requests from the same computer as the IIS server succeed if the AccessWrite property is set to true. You cannot set AccessNoRemoteWrite to false to enable remote requests, and set AccessWrite to false to disable local requests.