目录Getfiles检索错误的文件路径

时间:2013-08-31 07:23:46

标签: asp.net directoryinfo

我正在尝试访问我的应用程序中的路径。从frm_VerifyIndentity.aspx.cs到Imgs文件夹。

enter image description here

我正在使用下面的代码:

string pathRepositories = Request.ApplicationPath + "/IVT/Imgs/";

其中Request.ApplicationPath的值为

  

/ IVT

我将此参数传递给

DirectoryInfo directory = new DirectoryInfo(pathRepositories);
FileInfo[] files = directory.GetFiles("*.jpg");

pathRepositories是串联的地方:

  

/ IVT / IVT / IMGS /

但是,奇怪的是,因为当我检查目录所在的值时,值将在下面

C:\IVT\IVT\Imgs\ instead of my pathapplication.

这就是为什么我接下来的错误:

enter image description here

有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

来自documentation

  

使用此属性构建相对于应用程序根目录的URL   来自不在根目录中的页面或Web用户控件。

因此ApplicationPath为您提供相对路径,并将其视为绝对路径。

使用您当前的方法,您可能对HttpRequest.PhysicalApplicationPath感兴趣,但我不确定这是否合理。

另外,请考虑使用Path.Combine而不是自己连接字符串。