Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 274
user740144 Avatar asked Mar 27 '12 07:03

user740144


People also ask

How do I delete a file that doesn't exist?

Right click the bad file and click Add to Archives. Then in the options select to delete original file after compression. Leave other options same and proceed. Bad file will be deleted and a compressed file will be created in its place.

What exception will be thrown by deleting a file that does not exist?

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

How do you delete a file in Python if it exists?

To delete a file if exists in Python, use the os. path. exists() and os. remove() method.

How do you delete a file if already exists in C#?

The File. Delete(path) method is used to delete a file in C#. The File. Delete() method takes the full path (absolute path including the file name) of the file to be deleted.


1 Answers

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

like image 120
gdoron is supporting Monica Avatar answered Sep 21 '22 15:09

gdoron is supporting Monica