如何在C#中查找IIS站点id?

如何在C#中查找IIS站点id?,c#,.net,iis,installation,wmi,C#,.net,Iis,Installation,Wmi,我正在为我的web服务编写一个安装程序类。在许多情况下,当我使用WMI时(例如,创建虚拟目录时),我必须知道站点ID以提供站点的正确metabasePath,例如: metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]" for example "IIS://localhost/W3SVC/1/Root" metabasePath的格式为

我正在为我的web服务编写一个安装程序类。在许多情况下,当我使用WMI时(例如,创建虚拟目录时),我必须知道站点ID以提供站点的正确metabasePath,例如:

metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
for example "IIS://localhost/W3SVC/1/Root" 
metabasePath的格式为“IIS://根[/]”
例如“IIS://localhost/W3SVC/1/Root”

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

可能不是最好的方法,但有一种方法:

  • 循环浏览“IIS://servername/service”下的所有站点
  • 对于每个站点,请检查您的案例中的名称是否为“默认网站”
  • 如果为true,则您具有站点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
    

    下面是如何按名称获取它。您可以根据需要进行修改

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

    您可以通过检查属于元数据库路径
    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);
            }
        }
    }
    
    string siteToFind=“默认网站”;
    //林克之路
    使用(DirectoryEntry w3svc1=new DirectoryEntry(“IIS://Localhost/W3SVC”))
    {
    IEnumerable children=
    w3svc1.Children.Cast();
    变量站点=
    (摘自《儿童》杂志)
    哪里
    de.SchemaClassName==“IIsWebServer”&&
    de.Properties[“ServerComment”].Value.ToString()==siteToFind
    选择de).ToList();
    如果(sites.Count()>0)
    {
    //找到匹配项…假设ServerComment是唯一的:
    Console.WriteLine(站点[0].Name);
    }
    }
    //老路
    使用(DirectoryEntry w3svc2=new DirectoryEntry(“IIS://Localhost/W3SVC”))
    {
    foreach(w3svc2.Children中的DirectoryEntry de)
    {
    如果(de.SchemaClassName==“IIsWebServer”&&
    de.Properties[“ServerComment”].Value.ToString()==siteToFind)
    {
    //找到匹配项
    Console.WriteLine(de.Name);
    }
    }
    }
    

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

    它是否适用于IIS version@Helephant,其中使用此方法查找应用程序池??在IIS 6中??在我的系统上,我必须用以下内容更新上述内容,以使其编译“result=Convert.ToInt32(site.Name);”如果需要,返回的字符串可以解析为int。我的猜测是,在大多数情况下,您并不需要将其作为“int”返回,因为您将使用它来生成URI。
    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;
    }
    
    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;
            }