我怎么可以看一下IIS站点ID在C#中? [英] How can I look up IIS site id in C#?

查看:165
本文介绍了我怎么可以看一下IIS站点ID在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个安装程序类我的web服务。在许多情况下,当我使用WMI(如创建虚拟目录时)我要知道网站ID提供正确metabasePath到的部位,如:

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 metabasePath to the site, e.g.:

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

我怎么能看它采用C#编程的基础上,该网站的名称(如默认Web站点)?

How can I look it up programmatically in C#, based on the name of the site (e.g. for "Default Web Site")?

推荐答案

下面是如何通过名字来得到它。您可以根据需要修改。

Here is how to get it by name. You can modify as needed.

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站点ID在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆