Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.File.Move gives Exception "The file exists"

Tags:

.net

exception

Normally if you try move to a file that exists, File.Move throws a message "Cannot create a file when that file already exists".

I am getting a situation where File.Move throws

"The file exists"

This exception is thrown by System.IO.__Error.WinIOError directly after doing File.Move

It is attempting to move a file to a network drive.

After some searching, the only other thing which gives this particular message, seems to be Path.GetTempFileName() - which can throw this if the temp folder is full.

I'm not using GetTempFileName, and the temp folder is not full.

Does anyone know what may cause this, or how to troubleshoot? (this is on a managed server, for which only the managing company has access, and I can not show the proprietary code here).

like image 344
Leon van der Walt Avatar asked Sep 06 '13 14:09

Leon van der Walt


People also ask

How do you handle a file already exists in C#?

Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. Also, if the path is null, then this method returns false.

What is the use of the function system IO file exists filename when working with files?

Determines whether the given path refers to an existing directory on disk.

How to check if file exists or not in c#?

To check whether the specified file exists, use the File. Exists(path) method. It returns a boolean value indicating whether the file at the specified path exists or not.

Which of the following returns true if the file at the specified path exists or false otherwise?

Files. exists(): Returns true if the file exists; false if the file does not exist or its existence cannot be determined.


1 Answers

This comes out of Windows, different error codes for different scenarios. The second one is ERROR_ALREADY_EXISTS, "Cannot create a file when that file already exists" when the file is moved from one directory to another on the same drive. That's a pretty simple operation, it only requires moving the directory entry.

The first one is ERROR_FILE_EXISTS, "The file exists" when the file is moved from one drive to another drive. That's a much more involved operation, the file data has to be copied as well. In other words, it falls back to the equivalent of File.Copy(string, string, bool) with the last overwrite argument set to false. That it doesn't use the same error code is a bit of a quirk. The difference is a big deal to the file system driver, just not to your program. Otherwise the reason that you just get a pretty generic IOException instead of a more specific one that breaks down the file manipulation mishaps into finer-grained exceptions.

It isn't actually a problem because there isn't anything you can do about it in your code, you need the help of a human to correct the problem. Unless you take specific pre-emptive measures in your own code, either avoiding the move if the destination file already exists or by actually deleting the destination file first. Do note that neither is a 100% reliable workaround, there's a very small chance that another process creates the file again right after you deleted it but before you move. Making file operations completely reliable is pretty difficult on a multi-tasking operating system.

like image 114
Hans Passant Avatar answered Sep 18 '22 10:09

Hans Passant