Delete a File - "File does not exist"
-
Hey Guys, I'm working on a webapplication and I'm saving files .pdf in a folder called "Files" inside of the application project... So, I display these pdf files but I got a nasty problem, with delete them, It's driving me nuts.... because I every time I try to do it, I get this exception: "File does not exist", It couldn't be found... BUT the goddamned File does exist.. In fact, I display this pdf file (with response.transmitfile) with the same string path and It works... but when I try to delete it with the methos from IO.. It doesn't work at all.. Here' part my code: string path = @"\Files" + "\\" +filename.pdf; if (File.Exists(path)) { File.Delete(path); elimino = true; } So, anybody knows how can I solve this problem??? Thanks...
-
Hey Guys, I'm working on a webapplication and I'm saving files .pdf in a folder called "Files" inside of the application project... So, I display these pdf files but I got a nasty problem, with delete them, It's driving me nuts.... because I every time I try to do it, I get this exception: "File does not exist", It couldn't be found... BUT the goddamned File does exist.. In fact, I display this pdf file (with response.transmitfile) with the same string path and It works... but when I try to delete it with the methos from IO.. It doesn't work at all.. Here' part my code: string path = @"\Files" + "\\" +filename.pdf; if (File.Exists(path)) { File.Delete(path); elimino = true; } So, anybody knows how can I solve this problem??? Thanks...
phisco123 wrote:
string path = @"\Files" + "\\" +filename.pdf;
if (File.Exists(path))Maybe that's just a typo when you posted the message, but do you even have a variable named
filename.pdf
? What is the value of yourpath
variable? If the actual filename is left off, then the path would exist, but you would be trying to delete a directory, not an actual file. Of course, if it was just a typo in the message, it could be something else.CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
Hey Guys, I'm working on a webapplication and I'm saving files .pdf in a folder called "Files" inside of the application project... So, I display these pdf files but I got a nasty problem, with delete them, It's driving me nuts.... because I every time I try to do it, I get this exception: "File does not exist", It couldn't be found... BUT the goddamned File does exist.. In fact, I display this pdf file (with response.transmitfile) with the same string path and It works... but when I try to delete it with the methos from IO.. It doesn't work at all.. Here' part my code: string path = @"\Files" + "\\" +filename.pdf; if (File.Exists(path)) { File.Delete(path); elimino = true; } So, anybody knows how can I solve this problem??? Thanks...
Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want. If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:
// Assumes you're running this from within an ASP.NET Page
string root = Server.MapPath("~/Files");
string path = Path.Combine(root, filename);
if (File.Exists(path))
{
File.Delete(path);
}It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).
This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]
-
Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want. If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:
// Assumes you're running this from within an ASP.NET Page
string root = Server.MapPath("~/Files");
string path = Path.Combine(root, filename);
if (File.Exists(path))
{
File.Delete(path);
}It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).
This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]
Don't forget ... MS best practice[sic] is merely: try{ File.Delete(path); } catch{} Considering the file can be deleted between the File.Exist and File.Delete calls by another process.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Don't forget ... MS best practice[sic] is merely: try{ File.Delete(path); } catch{} Considering the file can be deleted between the File.Exist and File.Delete calls by another process.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
The user could also try setting a static setting of the directory location within his application under compile options and then just specify the file name which he is trying to delete. Try creating a setting, string string ie: string FilePath = "%USEPROFILE%.\\Desktop\\DirectoryToFile\\" (or where ever your path is located) Then in your application just add the Filename to the path, so that way you know that your application is pointing to the location, it would be concrete. Of course however if it is still saying that the file doesnt exists, make sure you are spelling it correctly and including the applications extension(which I'm not saying you aren't) and make sure you use the Try::catch, which is vital in this situation.
String FILE_TO_DELETE = FilePath+FileName;
Try
if.exists(FILE_TO_DELETE) then
FILE_TO_DELETE.delete
end if
catch ex as exception
msgbox("Unable to delete file. Reason: ", ex);
end tryAnd with the benefit of setting a static path in your program, you can change the path at anytime and save its settings easily. because as long as your code includes the "String FILE_TO_DELETE = FilePath+FileName;" That string will always change, just not your code.
-
Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want. If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:
// Assumes you're running this from within an ASP.NET Page
string root = Server.MapPath("~/Files");
string path = Path.Combine(root, filename);
if (File.Exists(path))
{
File.Delete(path);
}It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).
This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]
Heath Stewart wrote:
Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want.
If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:// Assumes you're running this from within an ASP.NET Page
string root = Server.MapPath("~/Files");
string path = Path.Combine(root, filename);
if (File.Exists(path))
{
File.Delete(path);
}It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).
Thanks man, That was it