Home / Articles / Programming Languages / C# / C# File Input Output III

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C# Programming Series

C# File Input Output III

Yashavant Kanetkar
Yashavant Kanetkar

Index

  1. Saving and Opening a File
  2. Attributes
Perhaps the only thing that works faster than the computer is computing jargon. It moves very, very fast. Often yesterday's rage is no longer today’s in–thing. Some jargon however has stuck around for long. GIGO (garbage in and garbage out) for instance. It has been a ground since the early days of computing. It has even matured over the years. Today GIGO happens automatically through a process called serialization…. A sure shot sign of man's progress!

Saving and Opening a File

If we wish to save the file we can click on 'Save' menu item. When we do so, save_Click( ) handler gets called. The handler is shown below:

private void save_Click(object sender, EventArgs e)
{
    SaveFileDialog sd = new SaveFileDialog();
    sd.Filter = "dat files ( *.dat ) | *.dat";  
    if (sd.ShowDialog() == DialogResult.OK)
    {
        FileInfo f = new FileInfo(sd.FileName);
        Stream st = f.Open(FileMode.Create, FileAccess.ReadWrite);
        foreach (shapes t in s)
            b.Serialize(st, t);
        st.Close();
    }
}

Here we have created an object of the SaveFileDialog class and used a "*.dat" filter for it. When we type in a file name and click OK, a FileInfo object gets created with the selected filename. The Open( ) function returns a Stream object associated with the file. We have collected it in a Stream reference. Then we have used a BinaryFormatter object to serialize the objects. This binary formatter is added as a data member of the form class as shown below:

BinaryFormatter b = new BinaryFormatter();

BinaryFormatter serializes and deserializes an object, in binary format. The Serialize( ) method of this class serializes the object to the given stream. After serializing all the elements of the array, we have closed the stream using the Close( ) method.

When we click 'Open' menu, the open_Click( ) event handler gets called. This handler is given below:

private void open_Click(object sender, EventArgs e)
{
           OpenFileDialog od = new OpenFileDialog();
           od.Filter = "dat files ( *.dat )|*.dat";
           if (od.ShowDialog() == DialogResult.OK)
            {
                FileInfo f = new FileInfo(od.FileName);
                Stream st = f.Open(FileMode.Open);
                while (st.Position != st.Length)
                    s.Add(b.Deserialize(st));
                st.Close();
            }
            Invalidate();
 }

Here firstly we have created an object of the OpenFileDialog class and displayed the dialog using the ShowDialog( ) method. In this method too we have created a FileInfo object and collected the corresponding Stream object of the specified file. Next we have used a while loop to deserialize the objects until the end of stream is reached. The Deserialize( ) method deserializes the specified stream into an object. We have collected these objects into our array by using the Add( ) method, closed the stream and called Invalidate( ) function for painting these shapes.

When we click 'New' menu item the following handler gets called:

 private void filenew_Click(object sender, EventArgs e)
{
     s.Clear();
     Invalidate();
}

Here we have cleared the array list by deleting all the elements in it using the Clear( ) method. After this we have called Invalidate( ). This time the method Form1_Paint( ) draws nothing as the array is empty, thereby resulting in a clean form.

On clicking the 'Exit' menu item the Dispose( ) method gets called and the form is disposed.

private void exit_Click(object sender, EventArgs e)
{
     Dispose();
}

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts