Create a website, application pool programmatically using C#

I have talked about IIS metabase and how to change HTTPErrors in IIS6 in my previous post.    This post will only give you sample code how you can programmatically

  • create website
  • create application pool and assign it to web site
  • set other website related properties
  • set ASP.NET version to web site

I have assumed that you have a basic understanding of IIS metabase and DirectoryEntry class if not please refer below articles.

Working with IIS Metabase

DirectoryEntry Class

System.DirectoryService Assembly

If you still have any doubt understanding the code, please leave a comment below.

1) Create a website

public void ProvisionSite()

string ipAddress = "";  //ipAddress of machine whose IIS we are going to access
string username = "shailesh";
string password = "patel";
//you need to supply username and password that has sufficient previllege to access IIS metabase

DirectoryEntry w3svc = new DirectoryEntry(String.Format("IIS://{0}/W3SVC",ipAddress),username,password);
DirectoryEntries sites = w3svc.Children;

//Add new website
DirectoryEntry siteDE = sites.Add("websiteIdentifier", (w3svc.SchemaClassName.Replace("Service", "Server")));
//replace "websiteIdentifier" with some uniqueId

//The ServerComment property specifies a comment for the website instance.
siteDE.Properties["ServerComment"][0] = iisSiteConfigDTO.Name;

//Commit Changes

//Set other metabase properties
siteRootDE = siteDE.Children.Add("Root", "IIsWebVirtualDir");
siteRootDE.Properties["AppFriendlyName"][0] = "websiteName";
siteRootDE.Properties["Path"][0] = @"\SharedWebsite1";
siteRootDE.Properties["AuthNTLM"][0] = true;

//if UNC Path is set, specify the username and password
siteRootDE.Properties["UNCUserName"][0] = "username";
siteRootDE.Properties["UNCPassword"][0] = "password";
siteRootDE.Properties["AccessScript"][0] = true;
siteRootDE.Properties["AccessFlags"][0] = 513;
siteRootDE.Properties["AppPoolId"].Value = GetAppPoolAssigment(siteRootDE, iisSiteConfigDTO, auditLog);
siteRootDE.Invoke("AppCreate", 1);

//find retrieve the website we have just created and start it
siteDE = new DirectoryEntry(string.Format(@"IIS://{0}/W3SVC/{1}", ipAddress , "websiteIdentifier"), userName, password );
siteDE.Invoke("Start", null);


2) Create Application Pool

public static string GetAppPoolAssigment(DirectoryEntry siteDE )

DirectoryEntry appPoolDE = null;

//if you want to assign existing application pool, get that
string appPoolPath = string.Format("IIS://{0}/W3SVC/AppPools/{1}", ipAddress, "Website1_Pool");
appPoolDE = new DirectoryEntry(appPoolPath, userName, password);

if (appPoolDE == null)
  DirectoryEntry appPoolRoot = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", ipAddress), userName, password);
  appPoolDE = appPoolRoot.Children.Add(appPoolName, "IIsApplicationPool");
  appPoolDE.Username = userName;
  appPoolDE.Password = password;

  appPoolDE.Properties["AppPoolIdentityType"][0] = 3;
  //custom worker process account
  appPoolDE.Properties["WAMUserName"][0] = workerProcessUserName;
  appPoolDE.Properties["WAMUserPass"][0] = workerProcessUserPassword;



return appPoolDE.Name;


3) Assign ASP.NET version to website

<pre>private static void SetASPNetVersion(DirectoryEntry siteDE)
   string AspNetV1 = "1.0.3705";
   string AspNetV11 = "1.1.4322";
   string AspNetV2 = "2.0.50727";
   string targetAspNetVersion = AspNetV2; ;

   //loop through the script maps
   for (int i = 0; i < siteDE.Properties["ScriptMaps"].Count; i++)
      //replace the versions if they exists
      siteDE.Properties["ScriptMaps"][i] = siteDE.Properties["ScriptMaps"][i].ToString().Replace(AspNetV1, targetAspNetVersion);
      siteDE.Properties["ScriptMaps"][i] = siteDE.Properties["ScriptMaps"][i].ToString().Replace(AspNetV11, targetAspNetVersion);
      siteDE.Properties["ScriptMaps"][i] = siteDE.Properties["ScriptMaps"][i].ToString().Replace(AspNetV2, targetAspNetVersion);


Hope this helps,


Posted in Microsoft Technology Tagged with:
11 comments on “Create a website, application pool programmatically using C#
  1. ultra says:

    Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

  2. tera says:

    Wow this is a great resource.. good article

  3. arifsharif says:

    where is the CreateAppPool() method definition

  4. Matt says:

    I cannot seem to find the code that acutally creates the application pool and assigns the .net version to the site.
    There are method calls but the methods themselves are missing:

    SetASPNetVersion(siteDE, iisSiteConfigDTO, auditLog);
    appPoolDE = CreateAppPool(appPoolName, iisSiteConfigDTO, auditLog);

  5. Shailesh says:

    @arifsharif, @Matt

    Actually those methods are not missing. I was trying to put code in separate method but haven’t done that. You can comment that line. I have updated post.

  6. M.Mimpen says:

    Thanks for sharing this method! At first I was trying to do this with ASP_REGIIS.EXE but the IIS will go down for about 10 seconds (even on a brand new, fast server!).

    Programmatically setting the ASP.NET version like this caused the IIS to not go offline and doesn’t need a restart.

    Lots of thanks :)

  7. PS says:

    I need to check status of application pool of a web server that is in the network and it works fine (using DirectoryEntry) in development environment but when I deploy it on the server, that is in the same network, gives me an error “Unknown error (0x80005000)”. Please help!!!

  8. Arul says:

    This code shows me some error. There is no declaration for siteRootDE and the following lines

    appPoolDE = CreateAppPool(appPoolName, iisSiteConfigDTO, auditLog);

    siteDE = new DirectoryEntry(string.Format(@”IIS://{0}/W3SVC/{1}”, ipAddress , “websiteIdentifier”), userName, password );

    Please help me i need this code for my project.

  9. mihir says:


    how can i add a website under the so that I get the url “localhost/”.

    This code will by default add the website at the top level i.e at the level.


  10. Sallydean says:

    Hi! Um i want to creat a website but i had no idear where to start from and i need your help i will be very greatful to your response to this and thanks for your time

  11. Linda says:

    Very nice, i suggest webmaster can set up a forum, so that we can talk and communicate.