title: IOException caused by file being in use
body: <p>I have a problem that I know people already asked here but I tried the solution they bring but it's not helping.</p>
  
  <p>My problem: I'm doing a program in C# with 2 forms. My main form is used to read a file .txt and put the information in a DataGridView: </p>
  
  <pre><code>public void LireFichier()
      {
          DataGridView dataGridView1 = new DataGridView();
  
          string delimeter = ";";
          string tableName = "Clients";
          string filePath = @"C:...\Clients.txt";
  
          DataSet dataset = new DataSet();
  
          using (StreamReader sr = new StreamReader(filePath))
          {
              dataset.Tables.Add(tableName);
              dataset.Tables[tableName].Columns.Add("ID");
              dataset.Tables[tableName].Columns.Add("Name");
              dataset.Tables[tableName].Columns.Add("LastName");
              dataset.Tables[tableName].Columns.Add("Datet");
              dataset.Tables[tableName].Columns.Add("Price");
              dataset.Tables[tableName].Columns.Add("Phone");
              dataset.Tables[tableName].Columns.Add("ID box");
  
              string allData = sr.ReadToEnd();
              string[] rows = allData.Split("\r".ToCharArray());
  
              foreach (string r in rows)
              {
                  string[] items = r.Split(delimeter.ToCharArray());
                  dataset.Tables[tableName].Rows.Add(items);
              }
              this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
           }
  </code></pre>
  
  <p>My second form is used to add a client in my .txt file :</p>
  
  <pre><code>      private void btn_Confirmer_Click(object sender, EventArgs e)
        {
          string filePath = @"...\Clients.txt";
          Client client = new Client();
  
          client.Id = idClient++;
          client.Name = tb_name.Text;
          client.LastName = tb_lastName.Text;
  
          client.Date = dtp_date.Value.ToString("dd-MMM-yyyy");
          client.Price = Convert.ToInt32(tb_price.Text);
          client.Phone = tb_telephone.Text;
          client.ID_Box = Convert.ToInt32(tb_idbox.Text);
  
          string clientInfo = client.Id.ToString() + ";" + client.Name.ToString() + ";" + client.LastName.ToString() + ";" + client.Date.ToString() + ";" +
                              client.Montant.ToString() + ";" + client.Phone.ToString() + ";" + client.ID_Boc.ToString() + ";";
  
          using (StreamReader sr = new StreamReader(filePath))
          {
              string allData = sr.ReadToEnd() + clientInfo;
              File.WriteAllText(filePath, allData);
          }
  
          this.Close();
        }
  </code></pre>
  
  <p>The problem remains in the StreamReader or the File.WriteAllText no matter what I do I always encounter the same exception(System.IO.IOException) that my file is in use when I arrived to write(add a client) in the file in my second form.</p>
  
  <p>The solution that I tried are:</p>
  
  <ol>
  <li>put the using blocks</li>
  <li>sr.Close()</li>
  <li>sr.Dispose() even if I know that the using block call dispose at the end.</li>
  <li>sr.Close() and sr.Dispose() at the end of the instruction.</li>
  </ol>