Home   Cover Cover Cover Cover
 

Copying a file

Chap4_Ex7.cs
using System;
using System.IO;

public class Copy {

  public static void Main(string[] args) {
    if(args.Length<2) Console.WriteLine("files?");
    else {
      if(!File.Exists(args[0])) Console.WriteLine("source file does not exist!");
      else {
        FileStream fsi = File.OpenRead(args[0]);
        FileStream fso = File.Create(args[1]);
        int b = fsi.ReadByte();
        while(b>0) {
          fso.WriteByte((byte)b);
          b = fsi.ReadByte();
        }
        fso.Close();
        fsi.Close();
      }
    }
  }
}