How to copy a file in C#

The Copy method copies an existing file to a new file on the specified location. The Copy method takes three parameters. First the original file with full path, second the file to be copied file name with the new path and third parameter that is optional that is used to overwrite an existing file. If third parameter is true, the Copy method will overwrite if file already exists.

The following code snippet copies the source file to the destination file. 

string sourceFile = @"C:\Temp\MaheshTX.txt";

string destinationFile = @"C:\Temp\Data\MaheshTXCopied.txt";

try

{

    File.Copy(sourceFile, destinationFile, true);

}

catch (IOException iox)

{

    Console.WriteLine(iox.Message);

}

Up Next
    Ebook Download
    View all
    Learn
    View all