问题描述
使用system.io.file类删除文件后:
After removing a file using the system.io.file class:
System.IO.File.Delete(openedPdfs.path);
如果文件已成功删除,我需要运行一些代码.只要该方法不返回任何值,我就在delete方法之后检查文件是否存在.如果仍然存在,我认为该操作将失败.
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.
问题是,删除方法可以正常工作,但是要删除的文件有几秒钟的时间. Exist函数返回true,因为在检查文件是否存在时.
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.
如何确定System.IO.File.Delete(openedPdfs.path);
是否成功完成?
How can I verify for sure if the System.IO.File.Delete(openedPdfs.path);
completed successfully?
代码:
FileInfo file = new FileInfo(openedPdfs.path);
System.IO.File.Delete(openedPdfs.path);
if (file.Exists == false)
{ ... }
else
{ ... }
如果未删除文件,
推荐答案
Delete
应该引发异常.因此,您对Exists
的调用是多余的.
Delete
should throw an exception if the file wasn't deleted. Hence, your call to Exists
is redundant.
请参阅文档> .
这篇关于如何检查System.IO.File.Delete是否成功删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!