如何查看System.IO.File.Delete删除文件是否成功

本文关键字:删除 文件 是否 成功 Delete File 何查看 System IO | 更新日期: 2023-09-27 17:50:38

使用system.io.file类删除文件后:

System.IO.File.Delete(openedPdfs.path);

如果文件被成功删除,我需要运行一些代码。只要该方法不返回任何值,我就检查删除方法后文件是否存在。如果它仍然存在,我认为操作失败了。

问题是,删除方法工作正常,但有几秒钟的文件被删除。Exist函数返回true,因为在检查文件是否存在时。

如何验证System.IO.File.Delete(openedPdfs.path);是否成功完成?

代码:

FileInfo file = new FileInfo(openedPdfs.path);    
System.IO.File.Delete(openedPdfs.path);
if (file.Exists == false)
{ ... }
else 
{ ... }

如何查看System.IO.File.Delete删除文件是否成功

正如其他人指出的那样,File.Delete方法将在失败的情况下抛出异常。

他们没有说明的是,在几乎所有情况下都会抛出异常,但在并非所有情况下都会抛出异常。

具体来说,如果要删除的文件碰巧不存在,File.Delete方法不会抛出异常。


  • 如果我们查看Microsoft官方文档System.IO.File.Delete,
  • ,如果我们跳过抛出的异常列表,
  • ,如果我们继续滚动到扩展代码示例以到达"备注"节中,
  • ,如果继续阅读这一节,尽管它主要由琐碎的,如
    • "指定带有任何相对或绝对路径的文件名;
    • 和"相对路径信息被解释为相对于当前工作目录"
    • 要获取当前工作目录,请参见GetCurrentDirectory"
  • 然后我们到达下面的小宝石,隐藏在噪音中:
  • 如果要删除的文件不存在,则不抛出异常。

    换句话说,这个函数故意内置了静默故障

    静默故障在几乎所有情况下都构成了对开发人员的破坏。

    Delete应该抛出一个异常,如果文件没有被删除。因此,对Exists的调用是多余的。

    查看Delete的文档

    这是Daniel A. White的回答的辅助:我们可以看到该方法的签名是public static void Delete(string path)。所以很明显,除非通过异常,否则您不会从Delete调用中获得反馈。但是,让我们假设您有一个由另一个进程定期写入或更新的文件:

    1. 程序成功删除文件。
    2. 其他进程在删除后立即重新创建。
    3. 您的程序使用file.Exists测试是否存在。有一个同名的新文件,所以返回true。严格来说,你走错了路。

    对于当前试图解决的问题,这种情况可能不正确,但是检查Delete调用是否抛出异常比依赖当前实现要健壮得多。

    你总是可以使用

     System.IO.File.Exists(path)
    

    虽然我同意Daniel的观点,但如果Delete不抛出异常,你应该很好。

    从评论和建议中,你应该能够使用以下方法来实现你想要的结果

    try {
      FileInfo file = new FileInfo(openedPdfs.path);    
      System.IO.File.Delete(openedPdfs.path);
      // if no exception is thrown then you should assume all has gone well and put  
      // your file successfully deleted code here.
    } catch /*(Specfic exceptions can be referenced here in separate catch blocks see Daniel A. White answer)*/ {
      // If something bad happened and the file was not deleted put handling code here
    } finally {
      // if some action needs to be taken regardless of whether the file was successfully deleted or not put 
      // that code here
    }
    

    如果文件不存在,不会抛出异常。在错误的情况下,它会抛出异常,如果它不能被删除,检查File.Delete

    我发现,如果你使用FileInfo Delete()实例方法,FileInfo实例属性Exists是不更新的。
    例如,下面的代码将抛出一个文件未找到异常,因为文件已被删除,但第二个if (output_file.Exists)仍然计算为true。

    FileInfo output_file;
    if (output_file.Exists) output_file.Delete();   
    FileStream fs;
    if (output_file.Exists)
    {
         fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite);
    }
    else
    {
         fs = new FileStream(fi.FullName, FileMode.CreateNew, FileAccess.ReadWrite);
    }
    

    我发现从旧的FileInfo创建一个新的FileInfo可以解决这个问题:

    FileInfo output_file;
    if (output_file.Exists)
    {
        output_file.Delete();
        output_file = new FileInfo(output_file.FullName);      
    }
    FileStream fs;
    if (output_file.Exists)
    {
         fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite);
    }
    else
    {
         fs = new FileStream(fi.FullName, FileMode.CreateNew, FileAccess.ReadWrite);
    }
    
    private String del(String fileLocation) {
     if (File.Exists(@fileLocation)) {
      try {
       File.Delete(@fileLocation);
      } catch (Exception e) {
       return "File couldn't be deleted because: " + e.GetType().Name;
      }
     } else {
      return "File doesn't exist";
     }
     return "File successfully deleted";
    }