Leon van der Walt
Leon van der Walt

Reputation: 1017

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

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

Upvotes: 8

Views: 6986

Answers (4)

Majid Shahabfar
Majid Shahabfar

Reputation: 4829

Using .NET Core 3.0 or above, the Move method has a new Boolian parameter to set the overwrite to true, which will replace the file if it exists.

Move(String, String, Boolean)

You can find more here about that.

Upvotes: 2

Gabriel Marius Popescu
Gabriel Marius Popescu

Reputation: 2196

There is another way to accomplish 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've tested this and it's working like a charm. Hope it helps somebody

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942438

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.

Upvotes: 4

noneabove
noneabove

Reputation: 89

I believe you have to specify the file name, assuming of course that you aren't. It helps to show your code, but I'll make the assumption you're doing:

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

Instead, try:

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

Upvotes: 2

Related Questions