当它位于调试文件夹中时,如何覆盖 visual basic 中的文本文件?
How do you overwrite a text file in visual basic whilst it's in the debug folder?
我在调试文件夹中有一个文本文件,我需要能够用新的、随机的、编辑过的文本覆盖它。问题是它不断提出:System.IO.IOException:'Bad file mode.'
我该如何解决这个问题?
代码如下:
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
我对这个编码还是陌生的,所以如果你能通过你的解决方案解释你的推理,我将不胜感激!
谢谢! :D
异常的原因是 OpenMode.Input
应该在 从文件读取 而不是写入文件时使用。由于您正在尝试覆盖该文件,因此您应该改用 OpenMode.Output
。
您也不需要重复相同的代码两次。您可以只检查 TextBox 是否为空,显示警告消息,然后写入文件。您的代码应如下所示:
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
话虽如此,作为 Andrew Mortimer has suggested in the comments,您应该熟悉标准的 .NET 文件读写方式
(即,使用 类 和 System.IO namespace. This guide 中的方法是一个好的开始。您会发现覆盖文本文件可以像这行代码一样简单:
IO.File.WriteAllText("enrolments.txt", txtToFile.Text)
我在调试文件夹中有一个文本文件,我需要能够用新的、随机的、编辑过的文本覆盖它。问题是它不断提出:System.IO.IOException:'Bad file mode.' 我该如何解决这个问题?
代码如下:
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
我对这个编码还是陌生的,所以如果你能通过你的解决方案解释你的推理,我将不胜感激! 谢谢! :D
异常的原因是 OpenMode.Input
应该在 从文件读取 而不是写入文件时使用。由于您正在尝试覆盖该文件,因此您应该改用 OpenMode.Output
。
您也不需要重复相同的代码两次。您可以只检查 TextBox 是否为空,显示警告消息,然后写入文件。您的代码应如下所示:
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
话虽如此,作为 Andrew Mortimer has suggested in the comments,您应该熟悉标准的 .NET 文件读写方式 (即,使用 类 和 System.IO namespace. This guide 中的方法是一个好的开始。您会发现覆盖文本文件可以像这行代码一样简单:
IO.File.WriteAllText("enrolments.txt", txtToFile.Text)