Reputation: 9081
I'd like to delete a zip file in my code c#
try
{
System.IO.File.Delete(@"C:/Projets/Prj.zip");
}
catch { }
but i have this error The format of the given path is not supported.
why this exception appears ? How can i fix this error?
Upvotes: 1
Views: 162
Reputation: 3548
You used forward slashes rather than backslahes, resulting in:
try
{
System.IO.File.Delete(@"C:\Projets\Prj.zip");
}
catch { }
It seems some odd character has slipped in somewhere making it invalid. If I copy/paste the line above, it gives me the same exception. However, if I remove the string and type it in by hand, it will give me a FileNotFound
(obviously).
Try cop/pasting this line:
System.IO.File.Delete(@"C:\Projets\Prj.zip");
After further investigation, the culprit appears to be a invsible character between the "
and the C
. Specifically, the unicode character for "left to right embedding" is present. If I convert the string to unicode, you can clearly see it:
System.IO.File.Delete(@"‪C:\Projets\Prj.zip");
Upvotes: 3
Reputation: 23646
Use Path
library, to access platform independent path manipulation. Example is given below:
var root = "C:" + Path.DirectorySeparatorChar;
var path = Path.Combine( root, "Projects", "Prj.zip" );
File.Delete(path); //will try to delete C:\Projects\Prj.zip
Upvotes: 1
Reputation: 2035
Try
string file = @"C:\Projets\Prj.zip";
if( System.IO.File.Exists(file))
System.IO.File.Delete(file);
Upvotes: 0
Reputation: 6372
File paths in Windows use backslashes, not forward slashes:
System.IO.File.Delete(@"C:\Projets\Prj.zip");
Upvotes: 2