Here is another quick example of serialising a few text fields - three in this case - I just created a standard from with three text boxes and a save button, this is the code behind the form.
A simple example of serialising - hope it helps
[Serializable]
public class Args
{
//Class to hold the data - marked as Serializable
public string a = string.Empty;
public string b = string.Empty;
public string c = string.Empty;
public Args()
{
}
}
//Save clicked
private void btSave_Click(object sender, EventArgs e)
{
Args args = new Args();
args.a = txt1.Text;
args.b = txt2.Text;
args.c = txt3.Text;
try
{
FileStream fs = new FileStream(@"C:\Test.bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, args);
fs.Close();
}
catch (FileNotFoundException fnfe)
{
MessageBox.Show(fnfe.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//This method is called once the form is open
public void LoadData()
{
try
{
FileStream fs = new FileStream(@"C:\Test.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
Args args = (Args)bf.Deserialize(fs);
txt1.Text = args.a;
txt2.Text = args.b;
txt3.Text = args.c;
fs.Close();
}
catch (FileNotFoundException fnfe)
{
MessageBox.Show(fnfe.Message);
}
catch (Exception e)
{
MessageBox.Show(ex.Message);
}
}