System.I.O进程无法访问该文件,因为它正被另一个进程使用

时间:2015-08-28 02:36:54

标签: vb.net

我正在设计一个计算得分平均值的程序。该程序工作得很好,除非我尝试将文本框中的名称和数字添加到文本文件中时返回错误。我相信每次使用它都会关闭文件。我的代码是:

Public Class Form4

Public Function GetNumberOfLines(ByVal file_path As String) As Integer 

    Using sr As New StreamReader("C:\file.txt")
        Dim NumberOfLines As Integer
        Do While sr.Peek >= 0 'a pre test loop to read lines
            sr.ReadLine()
            NumberOfLines += 1
        Loop
        Return NumberOfLines
        sr.Close() 'closes file after it is read
        sr.Dispose()
    End Using
End Function

Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Hide()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
    Dim FILE_NAME As String = "C:\file.txt"
    If System.IO.File.Exists(FILE_NAME) = True Then
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
        objWriter.Close()
        objWriter.Write(TextBox5.Text)
        MsgBox("Text written to file")
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Using sr As New System.IO.StreamReader("C:\file.txt")
        Dim LofInt As New List(Of Integer)
        While sr.Peek <> -1
            Dim line As String = sr.ReadLine
            Dim intValue As String = String.Empty
            For Each c As Char In line
                If IsNumeric(c) Then
                    intValue += c
                End If
            Next
            LofInt.Add(intValue)
        End While
        sr.Close()
        sr.Dispose()
        LofInt.Sort()
        For Each i In LofInt
            Scores.Items.Add(i)
        Next
        Dim sum As Decimal
        For Each decAdded As Decimal In Me.Scores.Items
            sum += Double.Parse(decAdded)
        Next
        TextBox4.Text = sum
        sr.Close()
        sr.Dispose()
    End Using
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\file.txt")
    TextBox2.Text = GetNumberOfLines("C:\file.txt")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sum As Decimal
    Dim avg As Decimal
    For Each decAdded As Decimal In Me.Scores.Items
        sum += Double.Parse(decAdded)
    Next
    avg = sum / (GetNumberOfLines("C:\file.txt")) 
    TextBox3.Text = avg
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Close()
End Sub

Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    TextBox1.Clear()
    TextBox2.Clear()
    Scores.Items.Clear()
    TextBox4.Clear()
    TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\file.txt")
    TextBox2.Text = GetNumberOfLines("C:\file.txt")
    Using sr As New System.IO.StreamReader("C:\file.txt")
        Dim LofInt As New List(Of Integer)
        While sr.Peek <> -1
            Dim line As String = sr.ReadLine
            Dim intValue As String = String.Empty
            For Each c As Char In line
                If IsNumeric(c) Then
                    intValue += c
                End If
            Next
            LofInt.Add(intValue)
        End While
        LofInt.Sort()
        For Each i In LofInt
            Scores.Items.Add(i)
        Next
        Dim sum As Decimal
        For Each decAdded As Decimal In Me.Scores.Items
            sum += Double.Parse(decAdded)
        Next
        TextBox4.Text = sum
        sr.Close()
        sr.Dispose()
    End Using
End Sub 

Dim objWriter As New System.IO.StreamWriter(FILE_NAME)是发生错误的地方

0 个答案:

没有答案