Get a Website Identifier from IIS6 using C#.Net

This function retrieves a website identifier from the IIS. Sometimes, you need a website identifier in order to connect to it and install something under it pragmatically. This function simply connects to the IIS and retrieve all the sub directories and then start searching for the desired website by its name and then return its identifier.


public static int GetWebSiteIdentifier(string server, string siteName)
{
   int siteIdentifier = 0;
   try
   {
     DirectoryEntry root = new DirectoryEntry("IIS://" + server + "/W3SVC");
     foreach (DirectoryEntry entry in root.Children)
     {
       if (string.Compare(entry.SchemaClassName, "IIsWebServer", true) == 0 &&      string.Compare(entry.Properties["ServerComment"].Value.ToString(), siteName, true) == 0)
       siteIdentifier = Int32.Parse(entry.Name);
     }
  }
  catch
  {
    throw new Exception("your message");
  }

  return siteIdentifier;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: