title: Write dictionary to file, retrieve dictionary from file
body: <p>I am writing a simple program in C# to add, delete, change, and view key-value pairs. I would like to keep the dictionary used in this code inside a file. I want to load this dictionary from the file when starting the program, and on exit save the edited dictionary.</p>
  <p>I have written both the code to write from the text file to the dictionary, and the code to write the dictionary to the text file on exit. Whenever I run the program with an empty text file it performs normally and adds any new entries to the dictionary. After exiting and saving these new dictionary key-values to the textfile, when I try to load the program again it seems that the key pair is already inside the dictionary and won't run the program.</p>
  <p>My dictionary has nothing inside of it to start with as well.</p>
  <p>The code to save the new dictionary into a text file is</p>
  <pre><code>private void btnExit_Click(object sender, EventArgs e)
  {
      File.WriteAllLines(&quot;myfile.txt&quot;,
      names.Select(x =&gt; &quot;[&quot; + x.Key + &quot;,&quot; + x.Value + &quot;]&quot;).ToArray());
      this.Close();
  }
  </code></pre>
  <p>Which will make the output in the textfile in this format</p>
  <pre>[jackP@gmail.com, Jack]
  [ashleyD@gmail.com, Ashley]
  [samF@gmail.com, Sam]</pre>
  <p>The code to output the updated textfile to the dictionary is</p>
  <pre><code>private void Form1_Load(object sender, EventArgs e)
  {
      StreamReader sr = new StreamReader(&quot;myfile.txt&quot;);
      string line;
  
      while ((line = sr.ReadLine()) != null)
      {
          string[] arr = line.Split(',');
          int i = 0;
          while (i &lt; arr.Length)
          {
              names.Add(arr[i], arr[i + 1]);
              i += 2;
          }
      }
  }
  </code></pre>