System.IO.File.Move gives a file exists exception

Usually, if you try to browse to a file that exists, File.Move displays the message "Cannot create file if this file already exists."

I get a situation where File.Move throws

"File exists"

This exception is thrown by System.IO.__Error.WinIOError immediately after executing File.Move

He is trying to move the file to a network drive.

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

I do not use GetTempFileName , and the temporary folder is not full.

Does anyone know what might cause this, or how to troubleshoot? (this is on a managed server, for which only the management company is available, and I can’t show the proprietary code here).

+5
source share
3 answers

This comes from Windows, different error codes for different scenarios. The second option is ERROR_ALREADY_EXISTS, "It is impossible to create a file if this file already exists" when the file is moved from one directory to another on the same drive. This is a fairly simple operation, you only need to move the entry in the directory.

The first one is ERROR_FILE_EXISTS, "File exists" when a file is moved from one drive to another. This is a much more complicated operation; file data must also be copied. In other words, it returns to the equivalent of File.Copy (string, string, bool) with the last rewrite argument set to false. That he does not use the same error code is a bit of a quirk. The difference is largely due to the file system driver, not your program. Otherwise, the reason why you just get a rather typical IOException, and not a more specific one, which destroys file manipulation errors into smaller exceptions.

Actually, this is not a problem, because there is nothing to be done in your code, you need the help of a person to fix the problem. If you are not taking specific preventive measures in your own code, avoid moving if the target file already exists or actually deletes the target file in the first place. Please note that none of them is a 100% reliable workaround, there is a very small chance that another process will create the file again immediately after it is deleted, but before it is moved. Making file operations completely reliable is quite difficult in a multitasking operating system.

+2
source

I believe that you should specify the file name, considering, of course, that this is not so. This helps show your code, but I will make an assumption that you are doing:

 File.Move(@"C:\Folder\File.txt", @"C:\Folder2") 

Try instead:

 File.Move(@"C:\Folder\File.txt", @"C:\Folder2\File.txt") 
+1
source

There is another way to do this:

 public void Move(string uncPath) { var files = Directory.EnumerateFiles(Constants.ROOT_PATH, "*.csv"); foreach (var file in files) { var item = file.GetAfter("\\"); File.Copy(file, uncPath + item); File.Delete(file); } } 

I checked it and it works like a charm. Hope this helps someone

0
source

Source: https://habr.com/ru/post/1500936/


All Articles