我正在为我的网络服务编写一个安装程序类。在许多情况下,当我使用 WMI 时(例如,创建虚拟目录时),我必须知道 siteId 才能为站点提供正确的配置数据库路径,例如:
metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
for example "IIS://localhost/W3SVC/1/Root"
如何根据站点名称(例如“默认网站”)在 C# 中以编程方式查找它?
以下是如何通过名称获取它。您可以根据需要进行修改。
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)