Fractal Fear
Fractal Fear

Reputation: 95

VB text file appending

I am writing a program for my class and it is to store data to a text file. Unfortunately it is only storing data for the first time the file is created and not appending new data to the text file. When I run the program and if I hit save after the file is created it does nothing, if I hit save again it crashes saying the file is currently in use. Any help would be appreciated. Also if there are any other things you see you could give me pointers on please let me know. Thanks.

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

Upvotes: 1

Views: 3033

Answers (2)

Tim
Tim

Reputation: 28530

Actually, File.AppendText will open a file for writing (in append mode), or create it if the file doesn't exist:

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.

So you can do away with the whole If statement. I would also use a Using block. Something like this:

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

The Using block will take care of closing and diposing the StreamWriter.

Upvotes: 3

Alexander
Alexander

Reputation: 2477

Your Sub Sub WriteRecordToFile just opens the file with the AppendText method. But all WriteLine calls are in the Else branch!

Upvotes: 2

Related Questions