title: Assign App pool to site in IIS6 using c#
body: <p>I am trying to create and assign application pool to a website in IIS6 and IIS7 using c#. I managed to do this for IIS7 <code>using Microsoft.Web.Administration;</code> but since its not supported in IIS6 i want to accomplish this using <code>System.DirectoryServices;</code>. I have managed to create the application pool but <strong>the assigning application pool does not work</strong>. </p>
  
  <p><strong>Here is what I have so far...</strong></p>
  
  <pre><code>   private static string DefaultAppPool = "DefaultAppPool";
     public static void SetApplicationPoolIIS6(string websiteName)
      {
          string metabasePath = "IIS://localhost/W3SVC";
  
          string appPoolName = GetSavedApplicationPoolName(websiteName);
  
          if (string.IsNullOrEmpty(appPoolName) || appPoolName.Equals(DefaultAppPool))
              appPoolName = websiteName + "AppPool";
  
          bool isAppPoolCreated = IsAppPoolCreatedIIS6(websiteName, appPoolName);
  
          if (!isAppPoolCreated)
              CreateApplicationPoolIIS6(appPoolName, metabasePath);
  
          DirectoryEntry w3svc = new DirectoryEntry(metabasePath);
          w3svc.RefreshCache();
  
          foreach (DirectoryEntry item in w3svc.Children)
          {
              string siteName = item.Properties["ServerComment"].Value == null ? "" : item.Properties["ServerComment"].Value.ToString();
  
              if (string.IsNullOrEmpty(siteName))
                  continue;
  
              if (siteName.Equals(websiteName))
              {
                  item.Invoke("Stop", null);
                  item.Properties["AppPoolId"].Value = appPoolName;
                  item.CommitChanges();
                  item.Invoke("Start", null);
                  break;
              }
          }
  
          w3svc.CommitChanges();
  
          DirectoryEntry w3svc2 = new DirectoryEntry(metabasePath);
          w3svc2.RefreshCache();
          SaveWebSiteSettingsIIS6(websiteName, appPoolName);
      }
  </code></pre>
  
  <p>Any idea why the below lines don't assign the created app pool ?</p>
  
  <pre><code>item.Properties["AppPoolId"].Value = appPoolName;
  item.CommitChanges();
  </code></pre>
  
  <p>Any help is much appreciated. Thank you.</p>