获取 IIS-7 及更高版本的网站 ID

Get Id of a website in IIS-7 and above(获取 IIS-7 及更高版本的网站 ID)
本文介绍了获取 IIS-7 及更高版本的网站 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过将 ID 作为参数以编程方式 (C#) 获取托管在 IIS-7(C:WindowsSystem32inetsrvconfig applicationHost.config) 中的任何网站的物理路径.但是对于iis7,通过代码查找Id失败.错误信息是

I am able to get physical path for any website hosted in IIS-7(C:WindowsSystem32inetsrvconfig 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):未知错误 (0x80005000)

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

即使添加 Microsoft.Web.Administration 程序集(http://cstruter.com/blog/306).

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

作为参考,这里是我用来获取 ID 的代码:

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

推荐答案

您是否尝试过使用新的 ServerManager 对象(通过 Microsoft.Web.Administration 可用于 IIS 7.0 及更高版本)?

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

特别是要获取一个站点的 ID,LINQ 的作用就像是一种魅力.

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

这篇关于获取 IIS-7 及更高版本的网站 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)