繁体   English   中英

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

[英]Deletion of compressed file in c# application

我想在我的代码C#中删除一个zip文件

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

但我有此错误The format of the given path is not supported.

为什么出现此异常? 我该如何解决此错误?

您使用了正斜杠而不是反斜杠,结果是:

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

似乎有些奇怪的字符进入某个地方使其无效。 如果我复制/粘贴上面的行,它会给我同样的例外。 但是,如果我删除该字符串并手动键入,它将给我一个FileNotFound (显然)。

尝试警察/粘贴此行:

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

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

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

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

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

使用Path库来访问平台无关的路径操作。 示例如下:

var root = "C:" + Path.DirectorySeparatorChar;

var path = Path.Combine( root, "Projects", "Prj.zip" );

File.Delete(path); //will try to delete C:\Projects\Prj.zip

尝试

 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