I've got a folder:
c:\test
I'm trying this code:
File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test");
I get exception:
File already exists
The output directory definitely exists and the input file is there.
I've got a folder:
c:\test
I'm trying this code:
File.Move(@"c:\test\SomeFile.txt", @"c:\test\Test");
I get exception:
File already exists
The output directory definitely exists and the input file is there.
Try
Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(Source, Destination, True)
. The last parameter is Overwrite switch, whichSystem.IO.File.Move
doesn't have.Personally I prefer this method. This will overwrite the file on the destination, removes the source file and also prevent removing the source file when the copy fails.
If file really exists and you want to replace it use below code:
You need to move it to another file (rather than a folder), this can also be used to rename.
Move:
Rename:
The reason it says "File already exists" in your example, is because
C:\test\Test
tries to create a fileTest
without an extension, but cannot do so as a folder already exists with the same name.What you need is:
or
This will either:
Edit: I should clarify my answer, even though it's the most upvoted! The second parameter of File.Move should be the destination file - not a folder. You are specifying the second parameter as the destination folder, not the destination filename - which is what File.Move requires. So, your second parameter should be
c:\test\Test\SomeFile.txt
.According to the docs for File.Move there is no "overwrite if exists" parameter. You tried to specify the destination folder, but you have to give the full file specification.
Reading the docs again ("providing the option to specify a new file name"), I think, adding a backslash to the destination folder spec may work.