asp.net知道iis网站名如何得到该网站物理路径

D6Space 2010-11-16 04:40:04
asp.net
知道了iis中网站的名称,如何得到该网站的物理路径,注意这里是知道了网站名称而不是虚拟目录名称,都是一个个单独的网站,
我的iis结构如下
网站标示 访问地址 物理路径
abc http://localhost:123 D:/abc
def http://localhost:456 D:/def
test http://localhost:789 D:/test

项目所在网站是 abc 如果知道了test 如何找到是 D:/test ?

不要告诉我是Server.MapPath()或者Request.ApplicationPath等,
...全文
393 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
烈火蜓蜻 2010-11-17
  • 打赏
  • 举报
回复
读取IIS配置文件
tone770 2010-11-17
  • 打赏
  • 举报
回复
引用下System.DirectoryServices.dll
using System.DirectoryServices;
tone770 2010-11-17
  • 打赏
  • 举报
回复
protected void Button3_Click(object sender, EventArgs e)
{
string strDir = GetWebVirtualDirectoryPath(GetWebSiteIdentifier("80"), "webone");
Response.Write("<br>" + strDir);
}

/// <summary>
/// 获取虚拟目录的物理路径
/// </summary>
/// <param name="identifier">虚拟目录所属网站的标识符</param>
/// <param name="name">虚拟目录名称</param>
/// <returns></returns>
private string GetWebVirtualDirectoryPath(string identifier, string name)
{
DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + name);
string path = (string)de.Properties["Path"].Value;

return path;
}

//// <summary>
/// 获取网站的标识符
/// </summary>
/// <param name="portNumber">端口号</param>
/// <returns></returns>
private string GetWebSiteIdentifier(string portNumber)
{
DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
foreach (object property in e.Properties["ServerBindings"])
{
if (property.Equals(":" + portNumber + ":"))
{
return e.Name;
}
}
}
}

// 默认为“默认网站”的标识符
return "1";
}
D6Space 2010-11-17
  • 打赏
  • 举报
回复
搞不定么?确定不可行?不行就只能写在配置里了!

我的iis结构如下
网站标示 访问地址 物理路径
abc http://localhost:123 D:/abc
def http://localhost:456 D:/def
test http://localhost:789 D:/test

项目所在网站是 abc 如果知道了test 如何找到是 D:/test ?
int64 2010-11-17
  • 打赏
  • 举报
回复
这个也能得到?
D6Space 2010-11-17
  • 打赏
  • 举报
回复
知道解决办法的帮下忙,谢了!
D6Space 2010-11-17
  • 打赏
  • 举报
回复
等答案,顶贴有分!
D6Space 2010-11-16
  • 打赏
  • 举报
回复
楼上两位没看题目,等答案!
by_封爱 版主 2010-11-16
  • 打赏
  • 举报
回复

string path = Request.ServerVariables["APPL_PHYSICAL_PATH"];
//虚拟服务绝对路径
lqkankan 2010-11-16
  • 打赏
  • 举报
回复
txtAPath.Text = Request.ServerVariables["APPL_PHYSICAL_PATH"];

txtAPath 是服务器控件 lable
g_lbz 2010-11-16
  • 打赏
  • 举报
回复
貌似不行,除非可以获取IIS中网站名和路径的对应关系

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧