Directory Getfiles retrieve wrong files path

344 Views Asked by At

I'm trying to access to the path in my application.From frm_VerifyIndentity.aspx.cs to Imgs folder.

enter image description here

I'm using next below code:

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

where the value of Request.ApplicationPath is

/IVT

And I pass this parameter to

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

Where the pathRepositories is concatenation:

/IVT/IVT/Imgs/

But, the strange, because when I check the value where the directory is looking for, the values is next below

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

Thta's why I get next below error:

enter image description here

Somebody know why?

1

There are 1 best solutions below

1
On

From the documentation:

Use this property to construct a URL relative to the application root from a page or Web user control that is not in the root directory.

So ApplicationPath gives you relative path, and you're treating it as an absolute path.

With your current approach you could be interested with HttpRequest.PhysicalApplicationPath, but I'm not sure if this is sound.

Also, consider using Path.Combine instead of concatenating strings yourself.