Copy a file : File Copy « File Directory Stream « C# / CSharp Tutorial






using System; 
using System.IO;  
 
class MainClass { 
  public static void Main(string[] args) { 
    int i; 
    FileStream fin; 
    FileStream fout; 
 
    try { 
      fin = new FileStream("inputFile.txt", FileMode.Open); 
    } catch(FileNotFoundException exc) { 
      Console.WriteLine(exc.Message + "\nInput File Not Found"); 
      return; 
    } 
 
    try { 
      fout = new FileStream("outputFile.txt", FileMode.Create); 
    } catch(IOException exc) { 
      Console.WriteLine(exc.Message + "\nError Opening Output File"); 
      return; 
    } 
 
    try { 
      do { 
        i = fin.ReadByte(); 
        if(i != -1) 
           fout.WriteByte((byte)i); 
      } while(i != -1); 
    } catch(IOException exc) { 
      Console.WriteLine(exc.Message + "File Error"); 
    } 
 
    fin.Close(); 
    fout.Close(); 
  } 
}








15.4.File Copy
15.4.1.Copy a file
15.4.2.Writing data: copying from one stream to another
15.4.3.Copying A File by using FileInfo