2013-07-02 122 views
1

我正在爲我的班級編寫一個程序,它將數據存儲到文本文件中。不幸的是,它只是在第一次創建文件時存儲數據,而不是將新數據附加到文本文件。當我運行該程序時,如果在文件創建完成後再次保存,它將不會執行任何操作,如果再次點擊保存,則會崩潰,並說文件當前正在使用。任何幫助,將不勝感激。如果還有其他的東西,你可以給我指點,請讓我知道。謝謝。附加VB文本文件

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 

回答

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 

Using塊會照顧關閉和diposing的StreamWriter

+0

我們還沒有學會使用它,但它工作得很好,還有一件事,當試圖做一個搜索時,它只會找到文本文件的第一部分並打開它,我放入的任何其他條目都會出現在我的meesagebox錯誤 –

+0

我不確定您的搜索問題是什麼意思。你可以發佈一個文件的例子和搜索中斷的地方嗎? (可能會更好地發佈爲新問題)。另外,你可以使用LINQ嗎? – Tim

+0

我將繼續併發佈一個新問題,而且我們還沒有,也不會在本課中學習任何關於LINQ的知識。謝謝您的幫助! –

2

你的副子WriteRecordToFile剛剛打開與AppendText方法的文件。但所有WriteLine電話都在Else分支!

+1

此外,'videoFile.Close()'和'ClearForm()'方法只在'Else'分支中。 – Tim