010 – C#. An example of demo application of copying files. Class FileStream.

An example of demo application of copying files. Class FileStream

 In the work is developed the application, which demonstrates the using of class FileStream to copy files. Also, the controls OpenFileDialog and SaveFileDialog are demonstrated.

The task

 You need to develop the application, which realizes the copying of files of any format. In the application must be specify the file-source and file-destination.



  1. Run MS Visual Studio.

Save the solution in any folder.


  1. Creating the form.

You need create the form as shown in Figure 1.

The controls, which are placed on the form, are following:

– three controls of type Button, which are named as “button1”, “button2”, “button3”;

– three controls of type Label, which are named as “label1”, “label2”, “label3”.

02_02_00_010_01_Fig. 1. The controls placed on the form


  1. Setting up of controls properties.

You need to form such properties of form “Form1”:

– property StartPosition = “CenterScreen”;

– property ControlBox = false;

– property Text = “Demo application of files copying”.

Also we form such properties of controls:

– in control “button1” the property Text = “Source file”;

– in “button2” the property Text = “Destination file”;

– in button3 the property Text = “Copy”;

– in label1 the property Text = ““;

– in label2 the property Text = “-”;

– in label3 the property Text = “-“.

 We correct the sizes and positions of controls as shown in Figure 2.

02_02_00_010_02eFig. 2. The application form after settings


  1. Controls OpenFileDialog and SaveFileDialog.

 From the “Toolbox” palette you need to place on the form two controls OpenFileDialog and SaveFileDialog from tab Dialogs (Fig. 3). After placing, we will get the two objects with names “openFileDialog1” and “saveFileDialog1”.

02_02_00_010_03eFig. 3. Controls OpenFileDialog and SaveFileDialog


  1. Programming the event Load of main form.

Listing of event handler Load of main form is following:

private void Form1_Load(object sender, EventArgs e)
  label1.Text = "None";
  label2.Text = "None";
  label3.Text = "";
  f_open = false;
  f_save = false;

 The event “Load” is called when application runs. In the event handle Form_Load() is realized the initialization of objects and variables.


  1. The event programming of clicking on the button1 (Source file).

 The selection of source file is carried out when user click on the button “Source file”.

 The listing of event handler of clicking on the button “button1” is following:

// Open File
private void button1_Click(object sender, EventArgs e)
  if (openFileDialog1.ShowDialog() == DialogResult.OK)
    label1.Text = openFileDialog1.FileName;
    f_open = true;
    label1.Text = "None";
    f_open = false;

 Control OpenFileDialog calls the standard window of Windows for selecting files. In application, to open the file, you need to call method ShowDialog(). After closing the window, if result of returning is equals “OK” then the name of selected file is saved in property FileName.

 Also, the flag f_open is set to value “true”. This means, that the selected source file.


  1. Programming of clicking on the button “button2” (Destination file).

To select the destination file os used the control saveFileDialog1.

 It calls the standard window of Windows, where you can select the path and name of file, which must be created (saved).

// save the file
private void button2_Click(object sender, EventArgs e)
  if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    label2.Text = saveFileDialog1.FileName;
    f_save = true;
    label2.Text = "None";
    f_save = false;

 Method ShowDialog() calls the window. If you selected the name of created file then returns “OK”. The full path of destination file is saved in the property FileName. Also, flag f_save is equals “true”. It means, that the destination file is selected.


  1. Programming of event of clicking on the button “button3” (Copy).

 To copy files are used FileStream class capabilities. This class is inherited from class Stream. The FileStream class provides the opportunity for single byte of input/output. It means that it can be applied to the files of any type, even to the executed files.

Previously you need to include the System.IO namespace.

using System.IO;

Listing of event handler of click on the button “button3” is following:

private void button3_Click(object sender, EventArgs e)
  // checking, whether the source file and the destination file are selected.
  if (!f_open || !f_save) return;
  FileStream fr = null; // fr – corresponds to source file
  FileStream fw = null; // fw – corresponds to destination file
  int x;
    // open the files
    fr = new FileStream(openFileDialog1.FileName, FileMode.Open);
    fw = new FileStream(saveFileDialog1.FileName, FileMode.Create);

    // copy files byte by byte
    x = fr.ReadByte();
    while (x != -1)
      x = fr.ReadByte();

    label3.Text = "OK!";
  catch (IOException exc)
    label3.Text = exc.Message; // if error was occured while copying
    // if all done, close the files
    if (fr != null) fr.Close();
    if (fw != null) fw.Close();

 To form a byte stream, which is attached to the file, two objects of class FileStream are created. One object corresponds to the destination file (fr), other object corresponds to the source file (fw).

 When you set access mode to the file is used values from enumeration FileMode.

 If you want to open the existing file for reading, then you need to set value


To create a new file, you need to set value


The enumeration FileMode has several other values.

There are two methods ReadByte() and Read() for reading bytes from file. In the application, to read one byte is used method

int ReadByte()

 If your attempt to open the file failed, then an exception is generated. If the file can not be opened through some input/output error then is generated IOException exception.

 To write byte into the file is used method

void WriteByte(byte value)

Parameter “value” sets byte, which is written.

 If an error occurs during recording, the generated IOException exception.

 After finishing work with file is called method Close().The method frees system resources allocated for this file.