This is a mirror of official site: http://jasper-net.blogspot.com/

Use C# to manage IIS

| Sunday, August 8, 2010
Copy Right:
  IISManager is a product of luckzj. Anyone could use this product for free but witout comercial purpose. If there’s any problem using IISManager, feel free to contact me with luckzj12@163.com. Or visit my website http://soft-bin.com.

The original address of this article is: http://www.soft-bin.com/html/2010/07/29/use-csharp-to-manage-iis.html

  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 a IIS management dll called IISManager and it’s 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 can not meet this requirement. However, you can also use IISManager to manage it under the condition that you don’t do operations unsurpported 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 manipulte 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 accept one parameter which represent an ADS node. I will use a 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):

DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");  
“IIS://localhost/W3SVC” is the name of IIS ADS.

We just created a IIS ADS server object, there may bye many websites on this node, we must find the website we are looking for:

private DirectroyEntry websiteEntry = null;
internal const string IIsWebServer = "IIsWebServer";

protected IISWebsite(DirectoryEntry Server)
{
   websiteEntry = Server;
}

public static IISWebsite OpenWebsite(string name)
{
   // get directory service
   DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");
   IEnumerator ie = Services.Children.GetEnumerator();
   DirectoryEntry Server = null;

   // find iis website
   while (ie.MoveNext())
   {
       Server = (DirectoryEntry)ie.Current;
       if (Server.SchemaClassName == IIsWebServer)
       {
           // "ServerComment" means name
           if (Server.Properties["ServerComment"][0].ToString() == name)
           {
               return new IISWebsite(Server);
               break;
            }
       }
   }

   return null;
}

Read more: Codeproject

Posted via email from .NET Info

0 comments: