...

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



alykins
10-15-2011, 07: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);
FileUpload1.SaveAs(SaveFilePath);
}


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)
File.Delete(filepath);


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-17-2011, 12:42 AM
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)
File.Delete(filepath);

No?

Old Pedant
10-17-2011, 12:45 AM
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.

alykins
10-17-2011, 03: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