dimanche 10 mai 2015

IO.StreamWriter and File is being used by another process

I am trying to put together a program that allows you to input names and grades into a textfile. From there, you will be able to filter them from grades A-F from a dropdown box and display them. Everything works as intended. However, I cannot figure out why the grades, after closing the application, saves to F. It is not properly saving the data. Also, when reopening the file and using it, I keep getting an error that the file is currently being in use by another process.

This is the code it returns to

If IO.File.Exists(fileName) Then

        output = IO.File.AppendText(fileName)
        output.WriteLine(list(counter))
        output.Close()
    Else

Here is the rest of the code that is currently in use.

Option Explicit On
Option Strict On
Option Infer Off

Public Class Form1

    'declare
    Dim fileName As String = "grades.txt"
    Dim studentName(1) As String
    Dim score(1) As String
    Dim list(1) As String
    Dim grade(1) As String
    Dim counter As Integer = 0

    Private Sub exitBtn_Click(sender As Object, e As EventArgs) Handles exitBtn.Click
        Me.Close()
        Dim input As IO.StreamReader
        input = IO.File.OpenText(fileName)
        input.Close()

    End Sub

    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'saves the list box information

        Dim outFile As IO.StreamWriter

        'open the file for ouput
        outFile = IO.File.CreateText("grades.txt")
        'write each name in the list box
        For Each name As String In nameListBox.Items
            outFile.WriteLine(name)
        Next name
        outFile.Close()
    End Sub

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



        If IO.File.Exists(fileName) Then
            Dim input As IO.StreamReader
            Dim data As String
            input = IO.File.OpenText(fileName)
            Do Until input.Peek = -1
                ReDim Preserve studentName(counter + 1)
                ReDim Preserve score(counter + 1)
                ReDim Preserve grade(counter + 1)
                ReDim Preserve list(counter + 1)

                data = input.ReadLine
                studentName(counter) = data.Substring(0, 35)
                score(counter) = data.Substring(35, 3)
                list(counter) = data
                retrieveGrade()

                counter += 1

            Loop

            input.Close()
        Else
            MessageBox.Show("Can't find the grades.txt file", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.Close()
        End If
    End Sub


    Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click

        nameListBox.Items.Clear()

        If ComboBox1.SelectedIndex = -1 Then
            For x As Integer = 0 To counter - 1
                nameListBox.Items.Add(studentName(x) & grade(x))
            Next
        Else
            Select Case ComboBox1.SelectedIndex
                Case 0
                    For x As Integer = 0 To counter
                        If grade(x) = "A" Then
                            nameListBox.Items.Add(studentName(x) & grade(x))
                        End If
                    Next
                Case 1
                    For x As Integer = 0 To counter
                        If grade(x) = "B" Then
                            nameListBox.Items.Add(studentName(x) & grade(x))
                        End If
                    Next
                Case 2
                    For x As Integer = 0 To counter
                        If grade(x) = "C" Then
                            nameListBox.Items.Add(studentName(x) & grade(x))
                        End If
                    Next
                Case 3
                    For x As Integer = 0 To counter
                        If grade(x) = "D" Then
                            nameListBox.Items.Add(studentName(x) & grade(x))
                        End If
                    Next
                Case 4
                    For x As Integer = 0 To counter
                        If grade(x) = "F" Then
                            nameListBox.Items.Add(studentName(x) & grade(x))
                        End If
                    Next
                Case 5
                    For x As Integer = 0 To counter - 1
                        nameListBox.Items.Add(studentName(x) & grade(x))

                    Next
            End Select
        End If
    End Sub

    Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click

        ReDim Preserve studentName(counter + 1)
        ReDim Preserve score(counter + 1)
        ReDim Preserve grade(counter + 1)
        ReDim Preserve list(counter + 1)

        studentName(counter) = nameTB.Text.PadRight(40)
        score(counter) = scoreTB.Text.PadRight(10)
        list(counter) = studentName(counter) & score(counter)
        retrieveGrade()
        Dim output As IO.StreamWriter

        If IO.File.Exists(fileName) Then

            output = IO.File.AppendText(fileName)
            output.WriteLine(list(counter))
            output.Close()
        Else
            MessageBox.Show("Can't find the grades.txt file", "File Error", MessageBoxButtons.OK)
            Me.Close()

        End If

        nameTB.Text = ""
        scoreTB.Text = "      "
        nameTB.Focus()
        counter += 1

    End Sub

    Private Sub retrieveGrade()

        Dim numberGrade As Integer
        Integer.TryParse(score(counter), numberGrade)
        If numberGrade >= 90 Then
            grade(counter) = "A"
        ElseIf numberGrade >= 80 Then
            grade(counter) = "B"
        ElseIf numberGrade >= 70 Then
            grade(counter) = "C"
        ElseIf numberGrade >= 60 Then
            grade(counter) = "D"
        Else
            grade(counter) = "F"

        End If
    End Sub
End Class

Would love some light shed on this problem. Thank you so much!

Aucun commentaire:

Enregistrer un commentaire