-5

I'm new to C# and I'm trying to make a List persist when relaunching the application. Everywhere I go I can't seem to find a simple way to do this, something like Python's Pickle. Any help would be appreciated thank you.

Alex G
  • 81
  • 3
  • 4

1 Answers1

1

The answer to this really depends on what exactly you want to save. Is it an actual List, as in List<> obejct? What does it contain? If it's something simple such as a List< string >, then do

        var list = new List<string>();
        list.Add("HELLO");
        list.Add("hi");

        // save
        using (var fs = new FileStream(@"F:\test.xml", FileMode.Create))
        {
            var serializer = new XmlSerializer(typeof(List<string>));
            serializer.Serialize(fs, list);
        }

        // read
        using (var s = new FileStream(@"F:\test.xml", FileMode.Open))
        {
            var serializer = new XmlSerializer(typeof(List<string>));
            List<string> result = (List<string>)serializer.Deserialize(s);
        }
Bogey
  • 4,926
  • 4
  • 32
  • 57