FileSystem.WriteAllText 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
寫入文字到檔案。
多載
| 名稱 | Description |
|---|---|
| WriteAllText(String, String, Boolean) |
寫入文字到檔案。 |
| WriteAllText(String, String, Boolean, Encoding) |
寫入文字到檔案。 |
WriteAllText(String, String, Boolean)
寫入文字到檔案。
public:
static void WriteAllText(System::String ^ file, System::String ^ text, bool append);
public static void WriteAllText(string file, string text, bool append);
static member WriteAllText : string * string * bool -> unit
Public Shared Sub WriteAllText (file As String, text As String, append As Boolean)
參數
- file
- String
要寫入的檔案。
- text
- String
文字要寫進檔案。
- append
- Boolean
True 附加於檔案內容; False 用來覆寫檔案內容。
例外狀況
該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。
file 是 Nothing。
檔案不存在。
該檔案被其他程序使用,或發生 I/O 錯誤。
路徑超過系統定義的最大長度。
路徑中的檔案或目錄名稱包含冒號(:),或格式無效。
記憶體不足以將字串寫入緩衝區。
使用者缺乏必要的權限來查看路徑。
範例
此範例會將該行 "This is new text to be added." 寫入檔案 Test.txt,覆蓋檔案中任何現有的文字。
My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt",
"This is new text to be added.", False)
此範例將資料夾中 Documents and Settings 檔案名稱寫入 FileList.txt,並在每個檔案間插入回車符號以提升可讀性。
For Each foundFile In
My.Computer.FileSystem.GetFiles("C:\Documents and Settings")
foundFile = foundFile & vbCrLf
My.Computer.FileSystem.WriteAllText(
"C:\Documents and Settings\FileList.txt", foundFile, True)
Next
備註
UTF-8 編碼用於寫入檔案。 若要指定不同的編碼方式,請使用方法的不同超載 WriteAllText 方式。
若指定的檔案不存在,則會建立該檔案。
若指定的編碼與檔案現有編碼不符,則忽略該編碼。
備註
該 WriteAllText 方法會打開一個檔案,寫入後再關閉它。 使用該 WriteAllText 方法的程式碼比使用 StreamWriter 物件的程式碼簡單。 然而,如果你是用迴圈來為檔案添加字串,物件 StreamWriter 的效能會更好,因為你只需要開啟和關閉檔案一次。 如需詳細資訊,請參閱 OpenTextFileWriter 方法。
下表列出與此 My.Computer.FileSystem.WriteAllText 方法相關的任務範例。
| 至 | 看! |
|---|---|
| 將文字寫入檔案 | |
| 將文字附加至檔案 |
另請參閱
適用於
WriteAllText(String, String, Boolean, Encoding)
寫入文字到檔案。
public:
static void WriteAllText(System::String ^ file, System::String ^ text, bool append, System::Text::Encoding ^ encoding);
public static void WriteAllText(string file, string text, bool append, System.Text.Encoding encoding);
static member WriteAllText : string * string * bool * System.Text.Encoding -> unit
Public Shared Sub WriteAllText (file As String, text As String, append As Boolean, encoding As Encoding)
參數
- file
- String
要寫入的檔案。
- text
- String
文字要寫進檔案。
- append
- Boolean
True 附加於檔案內容; False 用來覆寫檔案內容。
- encoding
- Encoding
寫入檔案時該用什麼編碼?
例外狀況
該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。
file 是 Nothing。
檔案不存在。
該檔案被其他程序使用,或發生 I/O 錯誤。
路徑超過系統定義的最大長度。
路徑中的檔案或目錄名稱包含冒號(:),或格式無效。
記憶體不足以將字串寫入緩衝區。
使用者缺乏必要的權限來查看路徑。
範例
此範例會將該行 "This is new text to be added." 寫入檔案 Test.txt,覆蓋檔案中任何現有的文字。
My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt",
"This is new text to be added.", False)
此範例將資料夾中 Documents and Settings 檔案名稱寫入 FileList.txt,並在每個檔案間插入回車符號以提升可讀性。
For Each foundFile In
My.Computer.FileSystem.GetFiles("C:\Documents and Settings")
foundFile = foundFile & vbCrLf
My.Computer.FileSystem.WriteAllText(
"C:\Documents and Settings\FileList.txt", foundFile, True)
Next
備註
若指定的檔案不存在,則會建立該檔案。
若指定的編碼與檔案現有編碼不符,則忽略該編碼。
備註
該 WriteAllText 方法會打開一個檔案,寫入後再關閉它。 使用該 WriteAllText 方法的程式碼比使用 StreamWriter 物件的程式碼簡單。 然而,如果你是用迴圈來為檔案添加字串,物件 StreamWriter 的效能會更好,因為你只需要開啟和關閉檔案一次。 如需詳細資訊,請參閱 OpenTextFileWriter 方法。
下表列出與此 My.Computer.FileSystem.WriteAllText 方法相關的任務範例。
| 至 | 看! |
|---|---|
| 將文字寫入檔案 | |
| 將文字附加至檔案 |