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

后端 未结 5 2005
别跟我提以往
别跟我提以往 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:09

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

提交回复
热议问题