简体   繁体   English

在C#应用程序中删除压缩文件

[英]Deletion of compressed file in c# application

I'd like to delete a zip file in my code c# 我想在我的代码C#中删除一个zip文件

try
 {
   System.IO.File.Delete(@"‪C:/Projets/Prj.zip");
  }
  catch { }

but i have this error The format of the given path is not supported. 但我有此错误The format of the given path is not supported.

why this exception appears ? 为什么出现此异常? How can i fix this error? 我该如何解决此错误?

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). 但是,如果我删除该字符串并手动键入,它将给我一个FileNotFound (显然)。

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: 经过进一步调查,罪魁祸首似乎是"C之间的一个不可见字符。具体来说,存在用于”从左到右嵌入”的unicode字符。如果将字符串转换为unicode,您可以清楚地看到它:

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

Windows中的文件路径使用反斜杠,而不是正斜杠:

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

Use Path library, to access platform independent path manipulation. 使用Path库来访问平台无关的路径操作。 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

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM