How can I look up IIS site id in C#?

后端 未结 5 2059
别跟我提以往
别跟我提以往 2020-12-28 22:49

I am writing an installer class for my web service. In many cases when I use WMI (e.g. when creating virtual directories) I have to know the siteId to provide the correct m

5条回答
  •  没有蜡笔的小新
    2020-12-28 23:11

    Here is how to get it by name. You can modify as needed.

    public int GetWebSiteId(string serverName, string websiteName)
    {
      int result = -1;
    
      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;
    }
    

提交回复
热议问题