Visual Basic - 不覆盖文本文件

时间:2014-04-25 07:34:39

标签: vb.net file overwrite

我正在编写一个程序,用户将获得一个分数和一个名字,这两个需要保存在内存中,然后显示在列表框中。例: 彼得保罗2000年

考虑到这一点,数据无法被覆盖。例: 彼得保罗2000 彼得约翰195 彼得杰克2301 彼得贝斯特7841

然后显示从最高到最小。

任何想法?

Current Coded在程序中用于写入和读取文件:

Private Sub OpenButton_Click(sender As Object, e As EventArgs) Handles OpenButton.Click
    Dim CountInteger As Integer
    Dim InStreamReader As New StreamReader("LeaderBoard.txt", False)
    ScoreListBox.Items.Clear()
    Do Until InStreamReader.Peek = -1
        InStreamReader.ReadLine()
        CountInteger += 1
    Loop
    InStreamReader.Close()
    CountInteger -= 1 'need ubound not size
    Dim InNewStreamReader As New StreamReader("LeaderBoard.txt", False)
    Dim DataArray(CountInteger) As String
    For i As Integer = 0 To UBound(DataArray)
        DataArray(i) = InNewStreamReader.ReadLine()
        ScoreListBox.Items.Add(DataArray(i))
    Next
    InNewStreamReader.Close()
End Sub

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
    Dim ResultDialogResult As DialogResult
    Dim LineString As String
    Dim InStreamReader As StreamReader
    Dim OutStreamWriter As StreamWriter

    With SaveFileDialog1
        .InitialDirectory = Application.StartupPath
        ResultDialogResult = .ShowDialog
        If ResultDialogResult = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        OutStreamWriter = New StreamWriter(.FileName)
    End With


    With OpenFileDialog1
        .DefaultExt = "txt"
        .Filter = "Text documents (*.txt)|*.txt"
        ResultDialogResult = .ShowDialog
        InStreamReader = New StreamReader(.FileName)
        Do Until InStreamReader.Peek = -1
            LineString = InStreamReader.ReadLine
            OutStreamWriter.WriteLine(LineString)
        Loop
        InStreamReader.Close()
        OutStreamWriter.Close()
    End With
End Sub

2 个答案:

答案 0 :(得分:0)

如果你打开这样的文件:

  OutStreamWriter = New StreamWriter(.FileName, true)

如果文件存在,您将附加到该文件

此方法会将字符串inStr附加到文件fileName

  Public sub saveTofile(string inStr, string fileName)

      OutStreamWriter = New StreamWriter(fileName, true)
      OutStreamWriter.WriteLine(inString)
      OutStreamWriter.Close()
  End Sub

谨防sintax错误,我无法尝试代码

答案 1 :(得分:0)

我不确定这会解决您的问题但是最佳做法是将Streamreader s(和Streamwriter s)包裹在Using Statement块中,以确保对象被处理掉。例如,重写OpenButton:

Private Sub OpenButton_Click(sender As Object, e As EventArgs) Handles OpenButton.Click
    Dim CountInteger As Integer
    Using InStreamReader As New StreamReader("LeaderBoard.txt", False)
        ScoreListBox.Items.Clear()
        Do Until InStreamReader.Peek = -1
            InStreamReader.ReadLine()
            CountInteger += 1
        Loop
        InStreamReader.Close()
    End Using
    CountInteger -= 1 'need ubound not size
    Using InNewStreamReader As New StreamReader("LeaderBoard.txt", False)
        Dim DataArray(CountInteger) As String
        For i As Integer = 0 To UBound(DataArray)
            DataArray(i) = InNewStreamReader.ReadLine()
            ScoreListBox.Items.Add(DataArray(i))
        Next
        InNewStreamReader.Close()
    End Using
End Sub

SaveButton

也是如此

对于实现IDisposable

的任何对象,您应该(只要有可能)执行此操作