tags:

views:

49

answers:

6

Hi I want

File.Copy(filePath, newPath);

to overwrite file file if it exists. Currenly it throws IOException.

+2  A: 

Then call the overload

File.Copy(filePath, newPath, true);
blowdart
+7  A: 

Use

File.Copy(filePath, newPath, true);

The third parameter is overwrite, so if you set it to true the destination file will be overwritten.

See: File.Copy in the MSDN

CodeInChaos
+1  A: 

Then use the other File.Copy(string, string, boolean). The third parameter indicates whether or not to overwrite the destination file if it exists (true if you want overwrite, false otherwise).

But what did you expect? If the function is designed to throw when the destination file exists, you need to find a way around that problem. So either:

  1. Search the documentation or Intellisense for an overload that does what you are asking.
  2. Barring that, create a wrapper around File.Copy(string, string) that deletes the destination file for you if it exists.
Jason
+1  A: 

From MSDN, you can do:

File.Copy(filePath, newPath, true);
Coding Gorilla
+1  A: 

There is an overload to this function that contains a third parameter. This parameter is called "overwrite" If you pass true, as long as the file is not Read Only, it will be overwritten.

davisoa
+1  A: 

File.Copy(filePath, newPath, bool overwrite)

does it.

elsni