title: Append text to existing file
body: <p>I am getting an exception stating that "It is being used by another process" while appending data to exiiting file</p>
  
  <p>Here is my code:</p>
  
  <pre><code> Dim tdate As String = Me.PresentDate.Value.ToString("MM/dd/yyyy")
      Dim Output As String = Directory.GetCurrentDirectory &amp; "\Output\Sample.txt"
      If System.IO.File.Exists(Output) Then
          IO.File.Delete(Output)
      End If
      Using myConnection As New SqlConnection("Data Source=sqldata;Initial Catalog=users;Persist Security Info=True;User ID=sa;Password=pwd"), myCommand As New SqlCommand("mySql command goes here", myConnection), adapter As New SqlDataAdapter(myCommand)
          myConnection.Open()
          ' Create the DataAdapter 
          Dim myDataAdapter As New SqlDataAdapter(myCommand)
  
          ' Create the DataSet
          Dim myDataSet As New DataSet
  
          ' Fill the DataSet
          myDataAdapter.Fill(myDataSet)
          Me.DataGridView1.DataSource = myDataSet.Tables(0)
          lblUploadFiles.Text = DataGridView1.Rows.Count - 1 &amp; "" &amp; " files Uploaded..."
          ' Close the connection
          myConnection.Close()
  
  
  
          ' create a writer and open the file
          Dim strDestinationFile As String
          strDestinationFile = Output
          Dim tw As TextWriter = New StreamWriter(strDestinationFile)
          For x As Integer = 0 To DataGridView1.Rows.Count - 2
              For y As Integer = 0 To DataGridView1.Columns.Count - 12
                  tw.Write(DataGridView1.Rows(x).Cells(12).Value)
                  If y &lt;&gt; DataGridView1.Columns.Count - 1 Then
                      tw.Write(Environment.NewLine)
                  End If
              Next y
              tw.WriteLine()
          Next x
          tw.Close()
      End Using
  
  
      Using myConnection2 As New SqlConnection("Data Source=sqldata;Initial Catalog=dbmyapps;Persist Security Info=True;User ID=sa;Password=pwd"), myCommand2 As New SqlCommand("my command goes here"))", myConnection2), adapter2 As New SqlDataAdapter(myCommand2)
          myConnection2.Open()
          ' Create the DataAdapter 
          Dim myDataAdapter2 As New SqlDataAdapter(myCommand2)
  
          ' Create the DataSet
          Dim myDataSet2 As New DataSet
  
          ' Fill the DataSet
          myDataAdapter2.Fill(myDataSet2)
          Me.DataGridView2.DataSource = myDataSet2.Tables(0)
          lblUploadFiles.Text = DataGridView2.Rows.Count - 1 &amp; "" &amp; " files Uploaded..."
          ' Close the connection
          myConnection2.Close()
  
  
  
          ' create a writer and open the file
          Dim strDestinationFile2 As String
          strDestinationFile2 = Output
          Dim tw2 As New System.IO.StreamWriter(strDestinationFile2, True)
          For x As Integer = 0 To DataGridView2.Rows.Count - 2
              For y As Integer = 0 To DataGridView2.Columns.Count
                  tw2.Write(DataGridView2.Rows(x).Cells(1).Value)
                  If y &lt;&gt; DataGridView2.Columns.Count - 1 Then
                      tw2.Write(Environment.NewLine)
                  End If
              Next y
              tw2.WriteLine()
          Next x
          tw2.Close()
          MsgBox("Successfully Exported")
      End Using
  </code></pre>
  
  <p>I have tried using this</p>
  
  <p><strong><code>File.AppendAllText(strDestinationFile2 , true)</code></strong> but this too gives me the same exception</p>