我正在为我的班级编写一个程序,它将数据存储到一个文本文件中。不幸的是,它只是在第一次创建文件时存储数据,而不是将新数据附加到文本文件中。当我运行程序时,如果我在创建文件后点击保存它什么也不做,如果我再次点击保存它会崩溃,说文件当前正在使用中。任何帮助,将不胜感激。另外,如果您看到任何其他事情,您可以给我指点,请告诉我。谢谢。
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