Copy a file in C Sharp/C# without using FileInfo

By | May 16, 2011

Hi,

We have discussed here copying a file using FileInfo. Now we are going to see how we can do the same without using FileInfo in C Sharp/C#.

using System;
using System.IO;

class MainClass {
  public static void Main(string[] args) {

    int i;
    FileStream fileIn;
    FileStream fileOut;

    try {
      fileIn = new FileStream("opFile.txt", FileMode.Open);
    }
    catch(FileNotFoundException exc) {
      Console.WriteLine(exc.Message + "nFile Not Found");
      return;
    }

    try {
      fileOut = new FileStream("clFile.txt", FileMode.Create);
    }
    catch(IOException exc) {
      Console.WriteLine(exc.Message + "nError Opening File");
      return;
    }

    try {
      do {
        i = fileIn.ReadByte();
        if(i != -1)
           fileOut.WriteByte((byte)i);
      } while(i != -1);
    }
    catch(IOException exc) {
      Console.WriteLine(exc.Message + "Error");
    }

    fileIn.Close();
    fileOut.Close();
  }
}

One thought on “Copy a file in C Sharp/C# without using FileInfo

  1. Pingback: Copying a file using FileInfo - C Sharp/C# | Coderz Heaven

Leave a Reply

Your email address will not be published. Required fields are marked *