【问题标题】:How can I look up IIS site id in C#?如何在 C# 中查找 IIS 站点 ID?
【发布时间】:2009-03-19 16:14:03
【问题描述】:

我正在为我的网络服务编写一个安装程序类。在许多情况下,当我使用 WMI(例如创建虚拟目录时)时,我必须知道 siteId 才能为站点提供正确的元数据库路径,例如:

metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
for example "IIS://localhost/W3SVC/1/Root" 

如何在 C# 中根据网站名称(例如“默认网站”)以编程方式查找它?

【问题讨论】:

    标签: c# .net iis installation wmi


    【解决方案1】:

    这是通过名称获取它的方法。您可以根据需要进行修改。

    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;
    }
    

    【讨论】:

    • 在我的系统上,我必须用以下内容更新上面的内容才能编译“result = Convert.ToInt32(site.Name);”
    【解决方案2】:

    您可以通过检查属于配置数据库路径IIS://Localhost/W3SVC 的子级的ServerComment 属性来搜索站点,这些子级的SchemaClassName 是IIsWebServer。

    以下代码演示了两种方法:

    string siteToFind = "Default Web Site";
    
    // The Linq way
    using (DirectoryEntry w3svc1 = new DirectoryEntry("IIS://Localhost/W3SVC"))
    {
        IEnumerable<DirectoryEntry> children = 
              w3svc1.Children.Cast<DirectoryEntry>();
    
        var sites = 
            (from de in children
             where
              de.SchemaClassName == "IIsWebServer" &&
              de.Properties["ServerComment"].Value.ToString() == siteToFind
             select de).ToList();
        if(sites.Count() > 0)
        {
            // Found matches...assuming ServerComment is unique:
            Console.WriteLine(sites[0].Name);
        }
    }
    
    // The old way
    using (DirectoryEntry w3svc2 = new DirectoryEntry("IIS://Localhost/W3SVC"))
    {
    
        foreach (DirectoryEntry de in w3svc2.Children)
        {
            if (de.SchemaClassName == "IIsWebServer" && 
                de.Properties["ServerComment"].Value.ToString() == siteToFind)
            {
                // Found match
                Console.WriteLine(de.Name);
            }
        }
    }
    

    这假定ServerComment 属性已被使用(IIS MMC 强制使用它)并且是唯一的。

    【讨论】:

      【解决方案3】:
      public static ManagementObject GetWebServerSettingsByServerComment(string serverComment)
              {
                  ManagementObject returnValue = null;
      
                  ManagementScope iisScope = new ManagementScope(@"\\localhost\root\MicrosoftIISv2", new ConnectionOptions());
                  iisScope.Connect();
                  if (iisScope.IsConnected)
                  {
                      ObjectQuery settingQuery = new ObjectQuery(String.Format(
                          "Select * from IIsWebServerSetting where ServerComment = '{0}'", serverComment));
      
                      ManagementObjectSearcher searcher = new ManagementObjectSearcher(iisScope, settingQuery);
                      ManagementObjectCollection results = searcher.Get();
      
                      if (results.Count > 0)
                      {
                          foreach (ManagementObject manObj in results)
                          {
                              returnValue = manObj;
      
                              if (returnValue != null)
                              {
                                  break;
                              }
                          }
                      }
                  }
      
                  return returnValue;
              }
      

      【讨论】:

      • 它是否适用于 IIS 版本
      • @Helephant,在哪里可以找到使用这种方法的应用程序池??在 IIS 6 中??
      【解决方案4】:
      private static string FindWebSiteByName(string serverName, string webSiteName)
      {
          DirectoryEntry w3svc = new DirectoryEntry("IIS://" + serverName + "/W3SVC");
          foreach (DirectoryEntry site in w3svc.Children)
          {
              if (site.SchemaClassName == "IIsWebServer"
                  && site.Properties["ServerComment"] != null
                  && site.Properties["ServerComment"].Value != null
                  && string.Equals(webSiteName, site.Properties["ServerComment"].Value.ToString(), StringComparison.OrdinalIgnoreCase))
              {
                  return site.Name;
              }
          }
      
          return null;
      }
      

      【讨论】:

      • 如果有必要,可以将返回的字符串解析为 int。我的猜测是,在大多数情况下,您并不需要将它作为“int”返回,因为您将使用它来创建 URI。
      【解决方案5】:

      也许不是最好的方法,但这里有一个方法:

      1. 遍历“IIS://servername/service”下的所有站点
      2. 检查每个站点的名称在您的情况下是否为“默认网站”
      3. 如果为真,那么您有您的网站 ID

      例子:

      Dim oSite As IADsContainer
      Dim oService As IADsContainer
      Set oService = GetObject("IIS://localhost/W3SVC")
      For Each oSite In oService
          If IsNumeric(oSite.Name) Then
              If oSite.ServerComment = "Default Web Site" Then
                  Debug.Print "Your id = " & oSite.Name
              End If
          End If
      Next
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多