HOW TO:在 Visual Basic 中附加至文字檔
藉由指定將 append 參數設定為 True,WriteAllText 方法可用於附加至文字檔。
若要附加至文字檔
請使用 WriteAllText 方法,指定目標檔案和要附加的字串,並將 append 參數設定為 True。
這個範例會將字串 "This is a test string." 寫入名為 Testfile.txt 的檔案。
Dim inputString As String = "This is a test string." My.Computer.FileSystem.WriteAllText( "C://testfile.txt", inputString, True)
穩固程式設計
以下條件可能會造成例外狀況:
路徑無效,由於下列原因之一:它是零長度字串,它僅包含空白,它包含無效的字元,或它是一個設備路徑 (以開始 \\。 \) (ArgumentException).
路徑無效,因為它是 Nothing (ArgumentNullException)。
File 指向不存在的路徑 (FileNotFoundException 或 DirectoryNotFoundException)。
檔案正由另一個程序使用中,或發生 I/O 錯誤 (IOException)。
路徑超過系統定義的最大長度 (PathTooLongException)。
路徑中的檔案或目錄名稱含有冒號 (:),或者是無效的格式 (NotSupportedException)。
使用者缺乏必要的使用權限來檢視路徑 (SecurityException)。