我可以通过将ID作为参数传递给IIS-7(C:\Windows\System32 32\inetsrv\config \applicationHost.config)中的任何网站(C#)以编程方式获取物理路径。但是,要通过代码找到Id,iis7将失败。错误信息是
COMException (0x80005000):未知错误(0x80005000)
即使在添加Microsoft.Web.Administration程序集(http://cstruter.com/blog/306)之后,也无法解决这个问题。
作为参考,下面是我用来获取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;
}发布于 2014-03-08 06:43:24
您是否尝试过使用新的ServerManager对象(通过Microsoft.Web.Administration可用于IIS7.0或更高版本)?
请尝试使用以下代码片段:
using (ServerManager serverManager = new ServerManager()) {
var sites = serverManager.Sites;
foreach (Site site in sites)
{
Console.WriteLine(site.Id);
}要获得某一站点的ID,LINQ的功能就像一种魅力。
https://stackoverflow.com/questions/22265585
复制相似问题