title: Get Id of a website in IIS-7 and above
body: <p>I am able to get physical path for any website hosted in IIS-7(C:\Windows\System32\inetsrv\config \applicationHost.config) programmatically (C#) by passing ID as a parameter. But to find Id through the code fails for iis7. The error message is </p>
  
  <p>COMException (0x80005000): Unknown error (0x80005000)</p>
  
  <p>which is not getting resolved even after adding Microsoft.Web.Administration assembly (<a href="http://cstruter.com/blog/306" rel="nofollow">http://cstruter.com/blog/306</a>). </p>
  
  <p>For reference, here is the code I use to get the ID:</p>
  
  <pre><code>    public static  int GetWebSiteId(string serverName, string websiteName)
      {
          int result = 2;
  
          DirectoryEntry w3svc = new DirectoryEntry(
                              string.Format("IIS://{0}/w3svc", serverName));
  
          foreach (DirectoryEntry site in w3svc.Children)
          {
              if (site.Properties["ServerComment"] != null)
              {
                  if (site.Properties["ServerComment"].Value != null)
                  {
                      if       (string.Compare(site.Properties["ServerComment"].Value.ToString(),
                                           websiteName, false) == 0)
                      {
                          result = int.Parse(site.Name);
                          break;
                      }
                  }
              }
          }
  
          return result;
      }
  </code></pre>