11

I have the following code which copies a file to a specific folder and then renames it. When a file with that name already exists I get the following exception:

Cannot create a file when that file already exists

Is there a way to overwrite the file and rename it? or I should delete the old one and then change the name?

Here is my code:

 File.Copy(FileLocation, NewFileLocation, true);
 //Rename:
 File.Move(Path.Combine(NewFileLocation, fileName), Path.Combine(NewFileLocation, "File.txt"));               
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
user1016179
  • 599
  • 3
  • 10
  • 24
  • 1
    Why do you need to delete the old one? Move should rename it "in place". – Jeff Feb 25 '14 at 13:18
  • http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp – Soner Gönül Feb 25 '14 at 13:18
  • As Jeff said, why delete it? Why not rename it or give your new file an incremental name? http://stackoverflow.com/questions/13049732/automatically-rename-a-file-if-it-already-exists-in-windows-way – Paul Zahra Feb 25 '14 at 13:23
  • @PaulZahra I think he means delete any existing file but it's not clear from the post. – Jeff Feb 25 '14 at 13:26
  • Please don't edit your code to have your answer, it was confusing enough without that, but now it looks like you've asked a question and the question already contains the answer, even more confusing! When putting your answer into your question at least mark it as an edit. – Paul Zahra Feb 25 '14 at 13:35
  • I need to have in that folder only one file with a specific name. (It is a license file which must has a specific name.) – user1016179 Feb 25 '14 at 13:36

6 Answers6

27

Try to use only:

if (File.Exists("newfilename"))
{
    System.IO.File.Delete("newfilename");
}

System.IO.File.Move("oldfilename", "newfilename");
Only a Curious Mind
  • 2,807
  • 23
  • 39
8

One simple option is to delete the file if it exists:

if (System.IO.File.Exists(newFile)) System.IO.File.Delete(newFile);
System.IO.File.Move(oldFile, newFile);

Something like that should work.

drew_w
  • 10,320
  • 4
  • 28
  • 49
6

You're correct, File.Move will throw an IOException if/when the filename already exists. So, to overcome that you can perform a quick check before the move. e.g.

if (File.Exists(destinationFilename))
{
    File.Delete(destinationFilename);
}
File.Move(sourceFilename, destinationFilename);
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 1
    Of course, based on the use case there's nothing that guarantees that there isn't a destination file there by the time you get to the move. – Jeff Feb 25 '14 at 13:30
4

You should use File.Exists rather than letting the Exception throw. You can then handle if the file should be overwrote or renamed.

Darren
  • 68,902
  • 24
  • 138
  • 144
  • But don't then assume it's going to succeed. Checking a priori is an optimization but not good control. – Jeff Feb 25 '14 at 13:23
2

Step 1 : as a first step identify wether the file exists or not before copying the file.
using File.Exists() method

Step 2: if the file already exists with same name then delete the existing file using File.Delete() method

Step 3: now copy the File into the new Location using File.Copy() method.

Step 4: Rename the newly copied file.

Try This:

string NewFilePath = Path.Combine(NewFileLocation, fileName);
if(File.Exists(NewFilePath))
{ 
File.Delete(NewFilePath);
}

//Now copy the file first
File.Copy(FileLocation, NewFileLocation, true);

//Now Rename the File
File.Move(NewFilePath, Path.Combine(NewFileLocation, "File.txt")); 
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
-1

I always use MoveFileEx with the flag MOVEFILE_REPLACE_EXISTING.

Limitations:

  1. It needs to use PInvoke, so it means your code will only work on the Windows platform.

  2. This flag MOVEFILE_REPLACE_EXISTING only work with File(Doesn't work with Folder)

If lpNewFileName or lpExistingFileName name a directory and lpExistingFileName exists, an error is reported.

MartinZ
  • 175
  • 2
  • 12