Check if a website exists on IIS6 using C#.Net

The following function checks if a website with the same name exists or not on the IIS. What it actually does, is that it connects to the IIS and retrieve all sub directories and then start searching for the specified website.


public static bool IsWebSiteExistsOnIIS(string serverName, string siteName)
{
   DirectoryEntry site = null;
   try
   {
      site = new DirectoryEntry("IIS://" + serverName + "/W3SVC");
      foreach (DirectoryEntry entry in site.Children)
      {
        if (entry.Properties["ServerComment"].Value != null &&
            entry.Properties["ServerComment"].Value.ToString() == siteName)
            return true;
      }
      return false;
   }
   finally
   {
      site.Dispose();
   }
}

Notice that the website name is saved in the “ServerComment” property and not the “name”. The “name” property holds the website Identifier.

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: