将文本附加到现有文件

时间:2013-08-22 12:52:38

标签: vb.net file append dispose

我收到一个异常,说明“正在被另一个进程使用”,同时将数据附加到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 个答案:

答案 0 :(得分: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()

this(dispose意外关闭流)

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