附加VB文本文件

时间:2013-07-02 05:35:06

标签: vb.net

我正在为我的班级编写一个程序,它将数据存储到文本文件中。不幸的是,它仅在第一次创建文件时存储数据,而不是将新数据附加到文本文件。当我运行该程序并且如果我在创建文件后点击保存它什么也没做,如果我再次点击保存它崩溃说文件当前正在使用。任何帮助,将不胜感激。如果还有其他任何事情你可以给我指点,请告诉我。感谢。

Imports System.IO

Public Class Main

    Const strFILENAME As String = "Videos.txt"

    Structure VideoData
        Dim videoName As String
        Dim runningTime As String
        Dim yearProduced As String
        Dim rating As String
    End Structure

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click

        Me.Close()
    End Sub

    Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

        Dim vertPos As Integer
        Dim formatStr As String = "{0,30}{1,15}{2,15}{3,10}"
        Dim video As VideoData
        Dim videoFile As System.IO.StreamReader

        If System.IO.File.Exists(strFILENAME) Then

            videoFile = System.IO.File.OpenText(strFILENAME)
            e.Graphics.DrawString("Video List", New Font("MS Sans Serif", 12, FontStyle.Regular), Brushes.Black, 10, 10)
            e.Graphics.DrawString("Date and Time: " & Now.ToString, New Font("MS Sans Serif", 12, FontStyle.Regular), Brushes.Black, 10, 10)
            e.Graphics.DrawString(String.Format(formatStr, "Video Name", "Year Produced", "Running Time", "Rating"), New Font("MS Sans Serif", 12, FontStyle.Regular), Brushes.Black, 10, 10)

            vertPos = 80

            Do Until (videoFile.Peek = -1)
                video.videoName = videoFile.ReadLine
                video.yearProduced = videoFile.ReadLine
                video.runningTime = videoFile.ReadLine
                video.rating = videoFile.ReadLine

                e.Graphics.DrawString(String.Format(formatStr, video.videoName, video.yearProduced, video.runningTime, video.rating), New Font("MS Sans Serif", 12, FontStyle.Regular), Brushes.Black, 12, vertPos)

                vertPos += 14
            Loop

            videoFile.Close()
        Else
            MessageBox.Show("Cannot open file.", "Error")
        End If
    End Sub

    Private Sub mnuFilePrint_Click(sender As Object, e As EventArgs) Handles mnuFilePrint.Click

        pdPrint.Print()
    End Sub

    Private Sub mnuFileSave_Click(sender As Object, e As EventArgs) Handles mnuFileSave.Click

        Dim video As New VideoData

        video.videoName = txtVideoName.Text
        video.runningTime = txtRunningTime.Text
        video.yearProduced = txtYearProduced.Text
        video.rating = txtRating.Text

        WriteRecordToFile(video)
    End Sub

    Sub WriteRecordToFile(ByRef video As VideoData)

        Dim videoFile As System.IO.StreamWriter

        If System.IO.File.Exists(strFILENAME) Then
            videoFile = System.IO.File.AppendText(strFILENAME)
        Else
            videoFile = System.IO.File.CreateText(strFILENAME)

            videoFile.WriteLine(video.videoName)
            videoFile.WriteLine(video.yearProduced)
            videoFile.WriteLine(video.runningTime)
            videoFile.WriteLine(video.rating)

            videoFile.Close()

            ClearForm()
        End If
    End Sub

    Sub ClearForm()

        txtVideoName.Text = " "
        txtYearProduced.Text = " "
        txtRunningTime.Text = " "
        txtRating.Text = " "
    End Sub

    Private Sub mnuSearchFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearchFind.Click

        Dim strName As String = InputBox("Enter the name of the video.", "Search")

        FindRecord(strName.Trim())
    End Sub

    Sub FindRecord(ByVal strName As String)

        Dim video As New VideoData
        Dim blnFound As Boolean = False
        Dim videoFile As System.IO.StreamReader

        If System.IO.File.Exists(strFILENAME) Then
            videoFile = System.IO.File.OpenText(strFILENAME)

            Do Until (videoFile.Peek = -1) Or blnFound
                video.videoName = videoFile.ReadLine
                video.yearProduced = videoFile.ReadLine
                video.runningTime = videoFile.ReadLine
                video.rating = videoFile.ReadLine

                If strName.ToUpper = video.videoName.ToUpper Then blnFound = True
            Loop
        End If

        videoFile.Close()

        If blnFound Then
            txtVideoName.Text = video.videoName
            txtYearProduced.Text = video.yearProduced
            txtRunningTime.Text = video.runningTime
            txtRating.Text = video.rating

            'MessageBox.Show(strName & " was not found.", "Record Not Found")
        Else
            MessageBox.Show("Cannot open file.", "Error")
        End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:3)

实际上,File.AppendText将打开一个文件进行写入(在追加模式下),或者如果该文件不存在则创建它:

Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

所以你可以取消整个If语句。我也会使用Using块。像这样:

Sub WriteRecordToFile(ByRef video As VideoData)

    Using videoFile As System.IO.StreamWriter = System.IO.File.AppendText(strFILENAME)

        videoFile.WriteLine(video.videoName)
        videoFile.WriteLine(video.yearProduced)
        videoFile.WriteLine(video.runningTime)
        videoFile.WriteLine(video.rating)

        ClearForm()
    End Using
End Sub

使用块将负责关闭和浸入StreamWriter

答案 1 :(得分:2)

您的子潜艇WriteRecordToFile只需使用AppendText方法打开文件。但所有WriteLine次调用都在Else分支中!