File.Delete doesn't throw error when the file doesn't exist

天涯浪子 提交于 2019-12-10 13:32:25

问题


Strange problem occurs on the production platform (64 bit win 2008 server). It is connected with File.Exists and File.Delete methods...

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 ...

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 ...

What is happening ... ?

I read somewhere that system virtualation could mess the things up ... but hasn't found any file in: %userprofile%\AppData\Local\VirtualStore


回答1:


File.Delete on MSDN:

public static void Delete(string path)

If the file to be deleted does not exist, no exception is thrown.

So. it's deliberated.... no true \ false and no exceptions, just delete the file if it exist.

Note about File.Exist:

public static bool Exists(string path)

Return Value Type: System.Boolean

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.

MSDN



来源:https://stackoverflow.com/questions/9884807/file-delete-doesnt-throw-error-when-the-file-doesnt-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!