Powered By Blogger

C#SHARP ANSWER

Wednesday, June 15, 2011

c# code to copy the file

using System;
using System.IO;

class FileCopy
{
  public static void Main() 
  {
    string sourceFile = @"C:\SourceFile.txt";
    string destinationFile = @"C:\DestinationFile.txt";

    //Attempt file copy
    try 
    {
      //Delete destinationFile for overwrite,
      //causes exception if already exists.
      File.Delete(destinationFile);

      File.Copy(sourceFile, destinationFile);
       
      //Successful if the method gets this far without causing an exception
      Console.WriteLine("Copy completed.");
    } 

    //If the file copy fails then catch will gain control of the method
    catch (Exception exc)
    {
      Console.WriteLine(exc.Message);
    }

    Console.WriteLine("\nPress Enter to Continue:");
    Console.Read();
  }
}