4

Can anyone help create an Application Pool in IIS using C#?

Once this has been done, how do I assign the Application Pool to a virtual directory, again using C#?

Kev
  • 118,037
  • 53
  • 300
  • 385
SCM
  • 2,065
  • 2
  • 14
  • 15

2 Answers2

12

If you're using IIS7:

To create an application pool using and set the .NET Framework version (to v2.0 in this case), do this:

using Microsoft.Web.Administration;
...

using(ServerManager serverManager = new ServerManager())
{
  ApplicationPool newPool = serverManager.ApplicationPools.Add("MyNewPool");
  newPool.ManagedRuntimeVersion = "v2.0";
  serverManager.CommitChanges();
}

You should add a reference to Microsoft.Web.Administration.dll which can be found in:

%SYSTEMROOT%\System32\InetSrv

To assign a virtual directory to an application pool (though I think you mean an application):

using (ServerManager serverManager = new ServerManager())
{
  // Find Default Website
  Site site = serverManager.Sites.First(s => s.Id == 1);
  Application newApp = site.Applications.Add("/MyNewApp", 
                                               @"C:\inetpub\wwwroot\mynewapp");
  newApp.ApplicationPoolName = "MyNewPool";
  serverManager.CommitChanges();
}

If you're using IIS6:

using (DirectoryEntry appPools = 
                   new DirectoryEntry("IIS://localhost/W3SVC/AppPools"))
{
  using (DirectoryEntry newPool = appPools.Children.Add("MyNewPool", 
                                                    "IIsApplicationPool"))
  {
    // Just use NetworkService as pool account
    newPool.Properties["AppPoolIdentityType"].Value = 2;
    newPool.CommitChanges();
  }
}

The following code creates an application called MyNewApp in the Default Web Site and assigns it to the application pool MyNewPool we created using the code example above:

using (DirectoryEntry siteRoot = 
         new DirectoryEntry(@"IIS://Localhost/W3SVC/1/root"))
{
  using (DirectoryEntry newApp = 
          siteRoot.Children.Add("MyNewApp", "IIsWebVirtualDir"))
  {
    newApp.Properties["Path"].Value = @"C:\inetpub\wwwroot\mynewapp";
    newApp.Properties["AccessScript"][0] = true;
    newApp.Properties["AccessFlags"].Value = 513; // AccessScript | AccessRead
    newApp.Properties["AuthFlags"].Value = 7;// AuthAnonymous|AuthBasic|AuthNTLM
    newApp.Properties["AppIsolated"].Value = "2";
    newApp.Properties["AppRoot"].Value = 
                       newApp.Path.Replace("IIS://Localhost", "/LM");
    newApp.Properties["AppPoolId"].Value = "MyNewPool";
    newApp.Properties["AppFriendlyName"].Value = "MyNewApp";
    newApp.CommitChanges();
  }
}

I all of the above cases your code needs to be running as an administrator.

For more information see:

IIS7:

IIS 7 Configuration Reference
How to Use Microsoft.Web.Administration

IIS6:

Using System.DirectoryServices to Configure IIS
IIS Programmatic Administration Reference
IIS Metabase Properties

Kev
  • 118,037
  • 53
  • 300
  • 385
0

I believe this depends on which version of IIS you are using but check out: http://msdn.microsoft.com/en-us/library/ms525598.aspx (example is for IIS7)

Source was from another question: Programmatically create a web site in IIS using C# and set port number

Community
  • 1
  • 1
Gibron
  • 1,350
  • 1
  • 9
  • 28