title: The process cannot access the file because it is being used by another process Streamwriter
body: <p>I am getting an error that my textfile that is used to create and save my dictionary is being used by another process, I have used Process explorer to no result on what could be using my file. Below is my code and the code throwing this error.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.IO;
  
  
  namespace meade_9_10
  {
      public partial class Form1 : Form
      {
  
          private Dictionary&lt;string, string&gt; names = new Dictionary&lt;string, string&gt;()
          {
              
          };
  
          public Form1()
          {
              //Make sure Form1 is loaded and ran on program open
              InitializeComponent();
              this.Load += Form1_Load;
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              //Grab myfile.txt and convert to array
              StreamReader sr = new StreamReader(&quot;myfile.txt&quot;);
              string line;
  
              while ((line = sr.ReadLine()) != null)
              {
                  string[] arr = line.Split(',');
                  int i = 0;
                  //Add array objects to names dictionary
                  while (i &lt; arr.Length)
                  {
                      names[arr[i]] = arr[i + 1];
                      i += 2;
                  }
              }
          }
  
          private void btnAdd_Click(object sender, EventArgs e)
          {   
              //declare variables
              string nameAdd;
              string emailAdd;
              //Put user input into variable
              nameAdd = txtNameAdd.Text;
              emailAdd = txtEmailAdd.Text;
              //Declare new dictionary key pair as user input
              names[emailAdd] = nameAdd;
              //clear the textbox controls
              txtNameAdd.Text = &quot;&quot;;
              txtEmailAdd.Text = &quot;&quot;;
          }
  
          private void btnDelete_Click(object sender, EventArgs e)
          {   
              string emailDel;
              emailDel = txtEmailDel.Text;
              //Remove key pair that is inputted by user
              names.Remove(emailDel);   
          }
  
          private void btnChange_Click(object sender, EventArgs e)
          {
              //Declare variables
              string emailDel;
              string nameAdd;
              string emailAdd;
              //Assign values to variables
              emailDel = txtEmailChange.Text;
              nameAdd = txtNameNew.Text;
              emailAdd = txtEmailNew.Text;
              //Delete the user inputted email to change
              names.Remove(emailDel);
              //Add the new key pair values to dictionary
              names.Add(emailAdd, nameAdd);
          }
  
          private void btnLookUp_Click(object sender, EventArgs e)
          {  
              //Declare variable
              string email = txtEmail.Text;
              //If statement to check if dictionary contains key value
              if (names.ContainsKey(email))
              {
                  outputName.Text = names[email];
                  outputEmail.Text = email;
              }   
          }
  
          private void btnExit_Click(object sender, EventArgs e)
          {
              //writes the names dictioanry to array inside text file
              File.WriteAllLines(&quot;myfile.txt&quot;,
              names.Select(x =&gt; x.Key + &quot;,&quot; + x.Value ).ToArray());
              
              //Closes the program
              this.Close();
          }
      }
  }
  </code></pre>
  <p>The part of my code giving me the error</p>
  <p>System.IO.IOException: 'The process cannot access the file 'C:\Users\Adrian\Desktop\ALL SCHOOL FILES\Fall 2021\C#\meade_9_10\bin\Debug\myfile.txt' because it is being used by another process.'</p>
  <p>is</p>
  <pre><code>            names.Select(x =&gt; x.Key + &quot;,&quot; + x.Value ).ToArray());
  </code></pre>
  <p>I just cannot figure out what process is using my text file that is breaking this program, it was working earlier and I haven't made any changes except for removing redundant white space between functions.</p>