问题描述
我可以通过将 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!