Home > database >  How do you overwrite a text file in visual basic whilst it's in the debug folder?
How do you overwrite a text file in visual basic whilst it's in the debug folder?

Time:05-04

I have a text file in the debug folder that I need to be able to overwrite with new, random, edited text. Problem is it keeps coming up with: System.IO.IOException: 'Bad file mode.' How do I fix this?

Here is the code:

Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
    If txtToFile.Text = "" Then
        If MsgBox("YOU ARE ABOUT TO SAVE THIS DOCUMENT AS A BLANK!!! Are you sure you want to override this file?", vbYesNo   vbQuestion   vbCritical, "WARNING!") = vbYes Then 'If the override button is clicked and the user has confirmed that they want to clear the file
            Dim FileNum As Integer = FreeFile()
            FileOpen(FileNum, "enrolments.txt", OpenMode.Input) 'open file
            PrintLine(FileNum, txtToFile.Text) 'write text to file
            FileClose(FileNum) 'close the file
        End If
    Else
        Dim FileNum As Integer = FreeFile()
        FileOpen(FileNum, "enrolments.txt", OpenMode.Input) 'open file
        PrintLine(FileNum, txtToFile.Text) 'write text to file
        FileClose(FileNum) 'close the file
        MessageBox.Show("File Written Over Successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub

I'm still new to this coding thing so if you could explain your reasoning through your solution that would be much appreciated! Thank you! :D

CodePudding user response:

The reason for the exception is that OpenMode.Input is supposed to be used when reading from the file, not writing to it. Since you're trying to overwrite the file, you should use OpenMode.Output instead.

You also don't need to repeat the same code twice. You could just check if the TextBox is empty, show the warning message, and then write to the file regardless. Your code should look something like this:

Dim textBoxIsEmpty As Boolean = (txtToFile.Text.Length = 0)
If textBoxIsEmpty Then
    If MsgBox("...", vbYesNo   vbQuestion   vbCritical, "WARNING!") <> vbYes Then Exit Sub
    ' Or...
    'If MessageBox.Show("...", "WARNING!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) <> DialogResult.Yes Then Exit Sub
End If

Dim FileNum As Integer = FreeFile()
FileOpen(FileNum, "enrolments.txt", OpenMode.Output) 'open file for writing
PrintLine(FileNum, txtToFile.Text) 'write text to file
FileClose(FileNum) 'close the file

If Not textBoxIsEmpty Then
    MessageBox.Show("File Written Over Successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

That being said, as Andrew Mortimer has suggested in the comments, you should get yourself familiar with the standard .NET ways of reading and writing to files (i.e., using classes and methods in the System.IO namespace. This guide is a good start. You'll find that overwriting a text file can be as simple as this single line of code:

IO.File.WriteAllText("enrolments.txt", txtToFile.Text)
  • Related