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

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

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

提交回复
热议问题