Using Copy( ) to Copy a File

Copy( ) copies the file specified by sourceFileName to the file specified by destFileName. The first version copies the file only if destFileName does not already exist. In the second form, if overwrite is passed true, the copy will overwrite the destination file if it exists. Both can throw several types of exceptions, including IOException and FileNotFoundException.
 
The following program uses Copy( ) to copy a file. Both the source and destination filenames are specified on the command line. Notice how much shorter this version is than the copy program shown earlier. It’s also more efficient.

Example

using System;
using System.IO;
namespace ConsoleApplication1
{
    class CopyFile
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: CopyFile From To");
                return;
            }
            // Copy the files.
            try
            {
                File.Copy(args[0], args[1]);
            }
            catch (IOException exc)
            {
                Console.WriteLine("Error Copying File\n" + exc.Message);
            }
        }
    }
}