问题描述
如何获得,通常看起来像的%SystemDrive%\inetpub\wwwroot
的路径?
How to get the path that usually looks like %SystemDrive%\inetpub\wwwroot
?
我想这是与 Microsoft.Web.Administration.ServerManager
类,但我找不到。办法
I guess it's something to do with Microsoft.Web.Administration.ServerManager
class, but I couldn't find a way.
更新:我想从独立的应用程序的路径。没有一个ASP.NET Web应用程序。
Update: I'm trying to get the path from standalone app. Not an ASP.NET web app.
推荐答案
要从一个独立的应用程序,你可以做以下发现一个网站的物理路径
To discover the physical path of a website from a standalone application you can do the following:
// If IIS7
// Add reference to Microsoft.Web.Administration in
// C:\windows\system32\inetsrv
using Microsoft.Web.Administration;
...
int iisNumber = 2;
using(ServerManager serverManager = new ServerManager())
{
var site = serverManager.Sites.Where(s => s.Id == iisNumber).Single();
var applicationRoot =
site.Applications.Where(a => a.Path == "/").Single();
var virtualRoot =
applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single();
Console.WriteLine(virtualRoot.PhysicalPath);
}
如果您正在使用IIS 6(或IIS7在IIS6管理兼容层)
If you're using IIS 6 (or the IIS6 admin compatibility layer for IIS7)
// If IIS6
// Add reference to System.DirectoryServices on .NET add ref tab
using System.DirectoryServices;
...
int iisNumber = 2;
string metabasePath = String.Format("IIS://Localhost/W3SVC/{0}/root", iisNumber);
using(DirectoryEntry de = new DirectoryEntry(metabasePath))
{
Console.WriteLine(de.Properties["Path"].Value);
}
这两个示例演示了如何发现路径的根的网站的
要探索的路径,你需要在必要时修改路径的虚拟目录。
To discover the path to a virtual directory you need to amend the paths as necessary.
这篇关于如何获得本地IIS服务器上的网站的物理路径? (从桌面应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!