Hi, i've been reading the other thread on serializing and use the following code to serialize the data in a textbox.

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            BinaryFormatter binaryFormat = new BinaryFormatter();
            Stream fstream = new FileStream("data.dat",
                FileMode.Create, FileAccess.Write, FileShare.None);
            binaryFormat.Serialize(fstream, (textBox1.Text));
            fstream.Close();

        }

This successfully creates a data.dat file in the app location, my question is how can i load that file and display the info back in the textbox?

Binary serialization is bad for your health unless you really have a need for it. I would recommend using XML Serialization. Here is an example of saving/loading with XML:

The class name is Categories and each Category has a Categories1 & Categories2 property. The class name is a little confusing with the member name.

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace daniweb
{
  public class Categories
  {
    private List<string> _category1;
    private List<string> _category2;

    public List<string> Category1
    {
      get { return _category1; }
      set 
      {
        if (value == null) return;
        _category1 = value; 
      }
    }

    public List<string> Category2
    {
      get { return _category2; }
      set 
      {
        if (value == null) return;
        _category2 = value; 
      }
    }

    public Categories()
    {
      _category1 = new List<string>();
      _category2 = new List<string>();
    }

    public void SaveToFile(string FileName)
    {
      if (File.Exists(FileName))
        File.Delete(FileName);
      XmlSerializer ser = new XmlSerializer(typeof(Categories));
      using (FileStream fs = new FileStream(FileName, FileMode.CreateNew))
      {
        ser.Serialize(fs, this);
      }
    }

    public static Categories LoadFromFile(string FileName)
    {
      XmlSerializer ser = new XmlSerializer(typeof(Categories));
      Categories result;
      using (FileStream fs = new FileStream(FileName, FileMode.Open))
      {
        result = (Categories)ser.Deserialize(fs);
      }
      return result;
    }

  }
}

I think i will stick with binary for the moment as i'm still new to this and xml looks more complicated. but thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.