Server.MapPath()没有获得所需的路径

Ovi*_*Ovi 1 asp.net iis directory server.mappath

我有一个asp.net网站,其中包含以下目录:

C:\用户\桌面\测试的\ src \网站

我有另一个名为"files"的文件夹,它位于:

C:\Users\Desktop\Testing\src\files
Run Code Online (Sandbox Code Playgroud)

从我的项目内部我试图从"文件"文件夹中读取文件,我这样做:

var path = HttpContext.Current.Server.MapPath("/files"); 
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

var path = HttpContext.Current.Server.MapPath(".."); 
Run Code Online (Sandbox Code Playgroud)

但它说 Failed to map the path '/files'.

这可能是什么原因?它可以用我的IIS做点什么吗?我怎么能让这个工作?

谢谢!

Dar*_*rov 5

你不能这样做.该Server.MapPath方法仅适用于相对于Web应用程序根目录的文件夹C:\Users\Desktop\Testing\src\website.当您离开此ASP.NET应用程序的控制域时,您无法使用此方法在层次结构中向上升级.要实现这一点,您必须使用绝对路径.例如,如果您想要读取位于应用程序之外的某个文件:

var data = File.ReadAllText(@"C:\Users\Desktop\Testing\src\files\somefile.txt");
Run Code Online (Sandbox Code Playgroud)