View Full Version : Resolved file path points to wrong folder dir

10-15-2011, 06:40 PM
I have a couple of functions... one (the one that is working) oploads a file to a local project folder... the code looks like this

string fileDirectory = @"/Files/";
string FilePath = Request.PhysicalApplicationPath;
if (FileUpload1.HasFile)
string fileName = txt_fileName.Text + ".html" ;
string SaveFilePath = FilePath + fileDirectory + Server.HtmlEncode(fileName);

Now this works fine. I am assuming that it is pointing to the correct folder due to the method of request.physicalapplicationpath... it then uses that call to point to where to write the file to. I then have another function to delete files from that folder

string[] filepaths = Directory.GetFiles(@"/Files/");
foreach (string filepath in filepaths)

now when i try this i get an error

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Files\'.

now this to me is obvious, i am calling the directory in directory.getfiles...
but i cannot figure out how to set my directory path relative to my application path...
IE how do i do something similar to request.physicalapplicationpath and apply it to my delete call... I've tried various methods but i get error after error... :confused:

Old Pedant
10-16-2011, 11:42 PM
You didn't use PhysicalApplicationPath in the second set of code!!!

So it *assumes* C:\ as the physical path, hence the error.

string rootPath = Request.PhysicalApplicationPath;
string fileDirectory = @"/Files/";
string basePath = rootPath + fileDirectory;
// debug to make sure basePath is correct

string[] filepaths = Directory.GetFiles(basePath);
foreach (string filepath in filepaths)


Old Pedant
10-16-2011, 11:45 PM
Oh, doh on me! You don't *HAVE* a valid Request in that second set of code, as that's running batch or some such, is that right???

You may want to use Server.MapPath in *both* cases, then. But, again, Sever is only valid for ASP.NET coding. If you are truly running a standalone application, then you have have to find another way.

10-17-2011, 02:10 PM
Thanks for getting back; I think your first post will resolve the issue. I didn't realize I wasn't setting the path until I looked at what you posted (feel like a putz now). Thank you thank you!! :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum