Copying a File - CSharp File IO

CSharp examples for File IO:File Command

Description

Copying a File

Demo Code

using System;//from  w w w  .j  ava 2 s  .  com
using System.IO;
class FileCopy
{
   public static void Main()
   {
      string[] CLA = Environment.GetCommandLineArgs();
      if ( CLA.Length < 3 )
      {
         Console.WriteLine("Format: {0} orig-file new-file", CLA[0]);
      }
      else
      {
         string origfile = CLA[1];
         string newfile  = CLA[2];
         Console.Write("Copy....");
         try
         {
            File.Copy(origfile, newfile);
         }
         catch (System.IO.FileNotFoundException)
         {
            Console.WriteLine("\n{0} does not exist!", origfile);
            return;
         }
         catch (System.IO.IOException)
         {
            Console.WriteLine("\n{0} already exists!", newfile);
            return;
         }
         catch (Exception e)
         {
            Console.WriteLine("\nAn exception was thrown trying to copy  file.");
            Console.WriteLine(e);
            return;
         }
         Console.WriteLine("...Done");
      }
   }
}

Result


Related Tutorials