Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Deletion of compressed file in c# application

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

Answers (4)

René Wolferink
René Wolferink

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

Ilya Ivanov
Ilya Ivanov

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

kostas ch.
kostas ch.

Reputation: 2035

Try

 string file = @"‪C:\Projets\Prj.zip";
  if( System.IO.File.Exists(file))
    System.IO.File.Delete(file);

Upvotes: 0

Sean Airey
Sean Airey

Reputation: 6372

File paths in Windows use backslashes, not forward slashes:

System.IO.File.Delete(@"C:\Projets\Prj.zip");

Upvotes: 2

Related Questions