Get Id of a website in IIS-7 and above(获取 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 IIS-7 及更高版本的网站 ID


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01