0

I have saved a dictionary with the follows:

 using (var file = File.OpenWrite("dictionary.txt"))
                foreach (KeyValuePair<string, string> entry in dictionary)
                {
                    streamwriter.WriteLine(entry.Key + entry.Value);
                }

How do I re-open this file and store the saved data back into a new dictionary?

UPDATE Answered

Teachme
  • 655
  • 1
  • 5
  • 10

1 Answers1

3

You need serialize it to save it, then deserialize it to load it.

Your code just writes each key-value pair to a file on a separate line. This won't work because there's no delimiter between the two. There is no way to know where the Key part stops and the Value part begins. You need a better method of serializing your dictionary.

There are many ways to do this, but just to name a few, you can use XML (see this SO question) or JSON (see this SO Question).

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331