How to Copy a File with Overwrite Properties

We can copy a file in two ways,

First way

using System.IO;
File.Copy(@"C:\abc.txt",@"D:\abc.txt");

This will copy a file from one position to another but when we copy again with same name and same directory , it gives following error.

The file 'D:\abc.txt' already exists.

To overcome from this error use 2nd way.

File.Copy(@"C:\logintime.txt",@"D:\logintime.txt",true);

This code will overwrite the new file with existing file.

Ebook Download
View all
Learn
View all