Creating an Application Pool on IIS6 using C#.Net

The following function creates an application pool on IIS6. The function creates an application pool and sets its identity with the specified user name and password.


public static void CreateAppPool(string serverName, string poolName, string userName, string password, bool enableRecylce)
{
  DirectoryEntry AppPool = null;
  DirectoryEntry root = null;
  try
  {
    root = new DirectoryEntry("IIS://" + serverName + "/W3SVC/AppPools");

    //create app pool
    AppPool = root.Invoke("Create", "IIsApplicationPool", poolName) as              DirectoryEntry;

   //set app pool identity
   AppPool.InvokeSet("AppPoolIdentityType", new Object[] {      AppPoolIdentityType.SpecificUser });
   AppPool.InvokeSet("WAMUserName", new Object[] { userName });
   AppPool.InvokeSet("WAMUserPass", new Object[] { password });
   AppPool.InvokeSet("AppPoolAutoStart", new Object[] { true });

   //set recycling options
   if (!enableRecylce)
     AppPool.InvokeSet("PeriodicRestartTime", new Object[] { 0 });

 

 

   AppPool.Invoke("SetInfo");
   AppPool.CommitChanges();
  }
  catch
  {
    throw new Exception("your message");
  }
  finally
  {
    AppPool.Dispose();
    root.Dispose();
  }
}

Advertisements

One Response to Creating an Application Pool on IIS6 using C#.Net

  1. Dave says:

    AppPoolIdentityType.SpecificUser <– where does this exist in the .NET namespace?

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: