0
好吧,我应该能够访问已经包含值的文件并将值写入该文件(即问题)。在Visual Basic 2010中写入顺序文件的新行
这是我到目前为止的代码:
Private Sub AddQuestion(sender As System.Object, e As System.EventArgs) Handles btnQuestions.Click
Dim pass, response, question As String
pass = "sample01"
response = InputBox("Please enter the administrator password.", "Password")
If response = pass Then
FileOpen(1, "W:\Visual Studio 2010\Projects\Culminating\assets\questions.txt", OpenMode.Output)
Do
question = InputBox("Enter new question.", "New Question")
If question = String.Empty Then
Exit Do
End If
Write(1, question)
WriteLine(1)
Loop
FileClose(1)
Else : MsgBox("Incorrect password. Please enter again.", MsgBoxStyle.Critical, "Incorrect Password")
End If
End Sub
然而,这增加了问题,我的文件,如果我退出输入框,然后再试一次,它将覆盖与新老问题。
FileOpen函数是一个非常古老的函数,主要用于向后兼容VB 6。如果您正在VB.NET中编写新代码(或者,看起来像* learning * VB.NET),那么您应该考虑这样做是现代的方式。那要么使用VB.NET特定的'My.Computer.FileSystem'对象来执行文件I/O,要么使用通用的.NET'System.IO.File'类。 –
@CodyGray谢谢。我想这样做,但是我们的老师对我们如何编写这些代码有特殊的要求,所以我试图按照他向我们展示过的方式去做。 – Alex
也许为您提供老师的具体要求,以便有人可以提供满足这些要求的答案? –