如何检查是否System.IO.File.Delete删除的文件成功地 [英] How to check if System.IO.File.Delete deleted a file sucessfully

查看:228
本文介绍了如何检查是否System.IO.File.Delete删除的文件成功地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除使用有System.IO.File类文件后:

  System.IO.File.Delete(openedPdfs 



。路径);



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



问题是,删除方法效果很好,但该文件的几秒钟被删除。该存在的函数返回true,因为当时它正在检查该文件是存在的。



我如何验证肯定,如果 System.IO。 ?File.Delete(openedPdfs.path); 成功地完成了



代码:

  FileInfo的文件=新的FileInfo(openedPdfs.path); 
System.IO.File.Delete(openedPdfs.path);
如果(file.Exists ==假)
{...}
,否则
{...}


解决方案

删除应该抛出一个异常,如果该文件未删除。因此,您的来电存在是多余的。



有一个看的删除


After removing a file using the system.io.file class:

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

I need to run some code if the file was sucessfully deleted. As long as the method does not return any value, I am checking if the file exist after the delete method. If it still exist I supposed the operation had failed.

The problem is, the deletion method works fine, but there is a couple of seconds to the file to be deleted. The Exist function return true because at the time it is checking the file is there.

How can I verify for sure if the System.IO.File.Delete(openedPdfs.path); completed sucessfully ?

Code:

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

解决方案

Delete should throw an exception if the file wasn't deleted. Hence, your call to Exists is redundant.

Have a look at the documentation for Delete.

这篇关于如何检查是否System.IO.File.Delete删除的文件成功地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆