using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public partial class SerializerTestForm1 : Form
{
    private String fileName = Directory.GetCurrentDirectory() + "\\output1.txt";

    public SerializerTestForm1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        String name = textBox1.Text;
        String address = textBox2.Text;
        int id = Convert.ToInt32(textBox3.Text);
        String courseInfo = textBox4.Text;      
        StudentInfo s = new StudentInfo(name,address,courseInfo,id);
        FileStream filestream = new
                   FileStream(fileName, FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(filestream, s);
        filestream.Close();
        button1.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        FileStream filestream2 = new FileStream(fileName,FileMode.Open);
        BinaryFormatter bf2 = new BinaryFormatter();
        StudentInfo si= new StudentInfo();
        si = (StudentInfo)bf2.Deserialize(filestream2);
        textBox8.Text = si.Name;
        textBox7.Text = si.Address;
        textBox6.Text = ""+si.ID;
        textBox5.Text = si.CourseInfo;
        filestream2.Close();

    }
}

[Serializable]
class StudentInfo
{
    public string Name, Address, CourseInfo;
    public int ID;

    public StudentInfo()
    {}

    public StudentInfo(String n, String a, String ci, int id)
    {
        Name = n; Address = a; CourseInfo = ci;
        ID = id;
    }
}