2016-06-10 51 views
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 

然而,這增加了問題,我的文件,如果我退出輸入框,然後再試一次,它將覆蓋與新老問題。

+1

FileOpen函數是一個非常古老的函數,主要用於向後兼容VB 6。如果您正在VB.NET中編寫新代碼(或者,看起來像* learning * VB.NET),那麼您應該考慮這樣做是現代的方式。那要麼使用VB.NET特定的'My.Computer.FileSystem'對象來執行文件I/O,要麼使用通用的.NET'System.IO.File'類。 –

+0

@CodyGray謝謝。我想這樣做,但是我們的老師對我們如何編寫這些代碼有特殊的要求,所以我試圖按照他向我們展示過的方式去做。 – Alex

+1

也許爲您提供老師的具體要求,以便有人可以提供滿足這些要求的答案? –

回答

2
FileOpen(1, "W:\Visual Studio 2010\Projects\Culminating\assets\questions.txt", OpenMode.Output) 

OpenMode.Output打開覆蓋內容的文件。你想要OpenMode.Append

+0

啊,我們的筆記說要使用'OpenMode.Output',所以我就這樣做了。非常感謝你,完美工作。 – Alex