C #: How to save a dictionary in a text file?

I read everywhere and cannot figure out how to save the dictionary in a TXT file. I know that you cannot just save the dictionary, you need to convert it to a string, but I am stuck with this. I have no idea how to convert it to a string, write it, and then read it and convert it from string to dictionary.

public static Dictionary<string, string> dict = new Dictionary<string, string>(); 
+4
source share
1 answer

Try serializing. You can serialize your dictionary in binary form and save it to a file. See BinaryFormatter on msdn . You could just:

 var binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(fileStream, dict); 

There is also an XmlSerializer class .

+1
source

Source: https://habr.com/ru/post/1495586/


All Articles