'File.Copy' does not overwrite a file

Using the following code, I am trying to overwrite a file if it exists. Currenly it throws IOException. How can I fix this problem?

File.Copy(filePath, newPath);

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


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.


Then call the overload

File.Copy(filePath, newPath, true);

From MSDN, you can do:

File.Copy(filePath, newPath, true);