文件不存在时File.Delete不会引发错误 [英] File.Delete doesn't throw error when the file doesn't exist
问题描述
生产平台(64位Win 2008服务器)上出现奇怪的问题.
它与File.Exists
和File.Delete
方法连接...
Strange problem occurs on the production platform (64 bit win 2008 server).
It is connected with File.Exists
and File.Delete
methods...
在调试(win xp)等测试平台上,它可以正常工作.在Server 2008上的前几次
File.Exists
报告了不存在的文件的true
...
On test platform in debug (win xp), etc it works fine. On Server 2008 for the first few times
File.Exists
reported true
for the file which didn't exist ...
当我进行进一步测试时,File.Exists
最终报告不存在的文件为false.
但是File.Delete
尝试删除不存在的文件时并没有引发任何错误...
When I Was doing further tests File.Exists
finally reported false for the file which didn't exist.
But File.Delete
hadn't thrown any error when attempting to delete the file which didn't exists ...
发生了什么...?
我在某处读到系统虚拟化可能会使事情混乱……但是在以下位置找不到任何文件:%userprofile%\AppData\Local\VirtualStore
I read somewhere that system virtualation could mess the things up ... but hasn't found any file in: %userprofile%\AppData\Local\VirtualStore
推荐答案
File.Delete
. aspx"rel =" noreferrer> MSDN :
File.Delete
on MSDN:
public static void Delete(string path)
如果要删除的文件不存在,则不会引发异常.
If the file to be deleted does not exist, no exception is thrown.
所以.它是经过深思熟虑的....没有true
\ false
且没有例外,请删除该文件(如果存在).
So. it's deliberated.... no true
\ false
and no exceptions, just delete the file if it exist.
关于File.Exist
的注意事项:
public static bool Exists(string path)
返回值类型:System.Boolean
Return Value Type: System.Boolean
true (如果调用者具有所需的权限,并且路径包含现有文件的名称);否则为
true .否则,为 false .如果path为Nothing,无效路径或长度为零的字符串,则此方法还返回 false .如果调用者没有足够的权限来读取指定的文件,则不会引发任何异常,并且无论是否存在路径,该方法都将返回false.
true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is Nothing, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.
这篇关于文件不存在时File.Delete不会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!