2013-08-22 127 views
0

我得到一個異常,指出「這是正在被另一個進程使用」,而將數據追加到exiiting文件文本追加到現有的文件

這裏是我的代碼:

Dim tdate As String = Me.PresentDate.Value.ToString("MM/dd/yyyy") 
    Dim Output As String = Directory.GetCurrentDirectory & "\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 & "" & " 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 <> 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 & "" & " 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 <> DataGridView2.Columns.Count - 1 Then 
        tw2.Write(Environment.NewLine) 
       End If 
      Next y 
      tw2.WriteLine() 
     Next x 
     tw2.Close() 
     MsgBox("Successfully Exported") 
    End Using 

我已經嘗試使用這

File.AppendAllText(strDestinationFile2 , true),但是這也給了我同樣的異常

+1

什麼行引發錯誤? – keyboardP

+0

@ keyboardP-在這一行:Dim tw2 As New System.IO.StreamWriter(strDestinationFile2,True) – coder

回答

1

始終使用Using語句,當您使用實現IDisposable的類時。

因此,例如,代替

Dim reader As New StreamReader(Output) 
reader = File.OpenText(Output) 

Using reader = File.OpenText(Output) 
    ' do something with it 
End Using 

或代替

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 <> DataGridView1.Columns.Count - 1 Then 
      tw.Write(Environment.NewLine) 
     End If 
    Next y 
    tw.WriteLine() 
Next x 
tw.Close() 

此(處置關閉隱含的流)

Using tw = 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 <> DataGridView1.Columns.Count - 1 Then 
       tw.Write(Environment.NewLine) 
      End If 
     Next y 
     tw.WriteLine() 
    Next x 
End Using 

或代替

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 <> DataGridView2.Columns.Count - 1 Then 
      tw2.Write(Environment.NewLine) 
     End If 
    Next y 
    tw2.WriteLine() 
Next x 

Using tw2 = 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 <> DataGridView2.Columns.Count - 1 Then 
       tw2.Write(Environment.NewLine) 
      End If 
     Next y 
     tw2.WriteLine() 
    Next x 
End Using 
+0

@ Tim-感謝您的時間......如果我使用上面的代碼,DataGridView1文件會添加兩次..但是我需要讀取到之前保存的文件結尾,然後從那裏添加並保存相同的文件。 – coder