title: The process cannot access the file '...' because it is being used by another process
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2730565/error-opening-streamreader-because-file-is-used-by-another-process">Error opening streamreader because file is used by another process</a>  </p>
  </blockquote>
  
  
  
  <p>In C#, I am trying to build a program that copies a certain amount of files or directories that are listed in a textfile from one computer to another on the same network (ex. from "\PC_OF_MARK" to "\PC_OF_SARAH") However, I seem to be stuck at opening a stream with a streamwriter after having opened and closed a StreamReader.</p>
  
  <p>Before I am able to copy anything, I need to be able to edit the contents of "files.txt" with my program.</p>
  
  <p>On my form, I have 4 buttons: Add, Delete, Close and Copy.
  I also have a ListBox lbItems that contains the lines from "files.txt".</p>
  
  <p>The program is located in the Delete function: 
  To delete a single line from "files.txt", I read the contents of lbItems and store them in a List. Next, I delete the string within the list that has the same index as the selected item in lbItems. Finally, I want to update "files.txt" by opening a StreamWriter (this is where the error occurs) and overwriting "files.txt" with the new List.</p>
  
  <p>"files.txt" contains these 2 lines:</p>
  
  <p>"%UserProfile%\Documents\files.txt" &amp;
  "%UserProfile%\Downloads\RubiksCube"</p>
  
  <pre><code>public partial class Form1 : Form
  {
  
      List&lt;string&gt; envKeys = new List&lt;string&gt;();
      List&lt;string&gt; envValues = new List&lt;string&gt;();
  
      public Form1()
      {
          InitializeComponent();
      }
  
      private void btnClose_Click(object sender, EventArgs e)
      {
          this.Close();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          IDictionary ev = Environment.GetEnvironmentVariables();
          foreach (DictionaryEntry de in ev)
          {
              envKeys.Add("%" + de.Key.ToString() + "%");
              envValues.Add(de.Value.ToString());
          }
          syncList();
      }
  
      private void syncList()
      {
          StreamReader r = new StreamReader(@"C:\Users\Eigenaar\Documents\files.txt");
          string line = r.ReadLine();
          while (line != "")
          {
              lbItems.Items.Add(line);
              line = r.ReadLine();
          }
          r.Close();
      }
  
      private void btnRemove_Click(object sender, EventArgs e)
      {
  
          if (lbItems.SelectedIndex != -1)
          {
              if (MessageBox.Show("Ben je zeker dat je " + lbItems.Items[lbItems.SelectedIndex] + " uit de lijst wil verwijderen?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes)
              {
                  List&lt;string&gt; array = new List&lt;string&gt;();
                  using (StreamReader r = new StreamReader(@"C:\Users\Eigenaar\Documents\files.txt"))
                  {
                      string line = r.ReadLine();
                      while (line != null)
                      {
                          array.Add(line);
                          line = r.ReadLine();
                      }
                      string t = r.ReadToEnd();
                  }
  
                  array.RemoveAt(lbItems.SelectedIndex);
  
                  //Error occurs here:
                  using (StreamWriter w = new StreamWriter(@"C:\Users\Eigenaar\Documents\files.txt", false))
                  //The process cannot access the file 'C:\Users\Eigenaar\Documents\files.txt' because it is being used by another process.
                  {
                      foreach (string str in array)
                      {
                          w.WriteLine(str);
                      }
                  }
                  lbItems.Items.RemoveAt(lbItems.SelectedIndex);
              }
          }
      } 
  }
  </code></pre>
  
  <p>If anyone knows what I'm talking about, please help!</p>