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;
      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;

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


Leave a Reply

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

You are commenting using your 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: