在IIS 7及以上的获取网站标识 [英] Get Id of a website in IIS-7 and above

查看:546
本文介绍了在IIS 7及以上的获取网站标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够获得物理路径在IIS-7(C:\Windows\System32\inetsrv\config \applicationHost.config)主办的网站的任何通过传递ID作为参数编程(C#)。但要找到标识通过代码IIS7失败。该错误消息



收到COMException(0x80005000):未知的错误(0x80005000)



这是没有得到,甚至解决加入Microsoft.Web.Administration组装后( http://cstruter.com/blog/306 )。



有关参考,在这里是我使用来获取ID代码:

 公共静态INT GetWebSiteId(字符串服务器名,字符串websiteName)
{
INT结果= 2;

的DirectoryEntry W3SVC =新的DirectoryEntry(
的String.Format(IIS:// {0} / W3SVC,服务器名));

的foreach(在w3svc.Children的DirectoryEntry网站)
{
如果(site.Properties [ServerComment]!= NULL)
{
如果(site.Properties [ServerComment]。值!= NULL)
{
如果(的String.Compare(site.Properties [ServerComment]。Value.ToString(),
websiteName ,FALSE)== 0)
{
结果= int.Parse(site.Name);
中断;
}
}
}
}

返回结果;
}


解决方案

您尝试使用?新的ServerManager的对象(仅适用于IIS 7.0及以上,通过Microsoft.Web.Administration)



请尝试使用下面的代码片断:

 使用(ServerManager的ServerManager的=新ServerManager的()){

变种网站= serverManager.Sites;
的foreach(在网站的网站网站)
{
Console.WriteLine(site.Id);
}

要获得一个站点的ID在paricular,LINQ就像一个魅力。


I am able to get physical path for any website hosted in IIS-7(C:\Windows\System32\inetsrv\config \applicationHost.config) programmatically (C#) by passing ID as a parameter. But to find Id through the code fails for iis7. The error message is

COMException (0x80005000): Unknown error (0x80005000)

which is not getting resolved even after adding Microsoft.Web.Administration assembly (http://cstruter.com/blog/306).

For reference, here is the code I use to get the ID:

    public static  int GetWebSiteId(string serverName, string websiteName)
    {
        int result = 2;

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

解决方案

Have you tried to use the new ServerManager object (available for IIS 7.0 and up, through Microsoft.Web.Administration)?

Please try using the following snippet:

using (ServerManager serverManager = new ServerManager()) { 

var sites = serverManager.Sites; 
foreach (Site site in sites) 
{ 
  Console.WriteLine(site.Id); 
}

To obtain the ID of one site in paricular, LINQ works like a charm.

这篇关于在IIS 7及以上的获取网站标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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