Create website on IIS6 using C#.Net

The following function creates a website on IIS6 with C#.Net using directory services:

public static void CreateWebSiteOnIIS(string serverName, string siteName, int port, int? sslPort, string sitePath, string appPoolName, AuthTypes authenticationType)

      //Create website on IIS
      DirectoryEntry root = new DirectoryEntry("IIS://" + serverName + "/W3SVC");
      int newWebSiteID = (int)root.Invoke("CreateNewSite", new object[] { siteName,           new object[] { ":" + port.ToString() + ":" }, sitePath });
      DirectoryEntry newSite = new DirectoryEntry("IIS://" + serverName + "/W3SVC/"           + newWebSiteID);

      //Set SSL port
      if (sslPort != null)
       newSite.Invoke("Put", new object[] { "SecureBindings", ":" + sslPort + ":" });

      //update application pool
      newSite.Invoke("Put", new object[] { "AppPoolId", appPoolName });

      //update the authentication type of the website
      newSite.Invoke("Put", new object[] { "AuthFlags", authenticationType });
      newSite.Invoke("Put", new object[] { "AccessRead", true });

      //save changes

      //Start the new site
      newSite.Invoke("Start", null);
      throw new Exception("Error");

You can choose any other authentication type for your website by selecting a different Authentication type from the following enum

public enum AuthTypes
     AuthAnonymous = 1,
     AuthBasic = 2,
     AuthNTLM = 4,
     AuthMD5 = 16,
     AuthPassport = 64


9 Responses to Create website on IIS6 using C#.Net

  1. mohamedfahmy252 says:

    You can check if the website exists or not before creating it.

  2. mohamedfahmy252 says:

    You can create an application pool and pass it to the create website method. see how to create an application pool

  3. mohamedfahmy252 says:

    You can check if the TCP Port is already in use or not before assigning the website a port. see how to check if a TCP port is already in use.

  4. sswedan says:

    Hi, this works fine for creating Web Site, but could you tell me what’s the code to create new FTP Sites?

    I’ve searched everywhere and I couldn’t find anything.


  5. peterriad says:

    how can I set the domain in case I select “Basic authontication”?

  6. arif says:

    Exception :Exception has been thrown by the target of an invocation.
    this is exception iam getting when this method is gettig called newSite.Invoke(“Start”, null);

    can u justify where iam going wrong or doi need to set any property in web.config

  7. smita says:

    DirectoryEntry w3svc = new DirectoryEntry(“IIS://localhost/w3svc”);

    //Create a website object array
    object[] newsite = new object[]{serverComment, new object[]{serverBindings}, homeDirectory};

    //invoke IIsWebService.CreateNewSite
    int websiteId = (int)w3svc.Invoke(“CreateNewSite”, newsite);

    DirectoryEntry neww3svc = new DirectoryEntry(“IIS://localhost/w3svc” + “/” + websiteId);

    neww3svc.Invoke(“start”, null);

    Statement: neww3svc.Invoke(“start”, null); hangs and an exception is thrown after 30/40 seconds.

    Can you please suggest whats worng in my code?

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: