FileSystemProxy.OpenTextFileWriter 方法

定義

開啟 StreamWriter 物件,以便寫入指定的檔案。

多載

OpenTextFileWriter(String, Boolean)

開啟 StreamWriter 物件,以便寫入指定的檔案。

OpenTextFileWriter(String, Boolean, Encoding)

開啟 StreamWriter,以便寫入指定的檔案。

OpenTextFileWriter(String, Boolean)

開啟 StreamWriter 物件,以便寫入指定的檔案。

public:
 System::IO::StreamWriter ^ OpenTextFileWriter(System::String ^ file, bool append);
public System.IO.StreamWriter OpenTextFileWriter (string file, bool append);
member this.OpenTextFileWriter : string * bool -> System.IO.StreamWriter
Public Function OpenTextFileWriter (file As String, append As Boolean) As StreamWriter

參數

file
String

要寫入的檔案。

append
Boolean

True 表示要將內容附加到檔案,False 表示要覆寫檔案的內容。 預設為 False

傳回

要寫入到指定檔案的 StreamWriter 物件。

例外狀況

檔案名稱以斜線結束。

範例

這個範例會使用 方法開啟 StreamWriter ,並用它來使用 類別的方法StreamWriter,將字串寫入文本檔WriteLineMy.Computer.FileSystem.OpenTextFileWriter

Dim file = My.Computer.FileSystem.OpenTextFileWriter(
    "c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()

備註

方法 OpenTextFileWriter 會開啟並初始化檔案的數據流,然後傳回 StreamWriter 該數據流的物件。 您可以視需要多次寫入至數據流,然後在完成時將其關閉。

注意

您必須在物件上StreamWriter呼叫 Close 方法,以確保所有數據都正確寫入基礎數據流。

如果您只將幾個字串寫入檔案中,使用 WriteAllText 方法可能會比較簡單。

下表列出涉及 My.Computer.FileSystem.OpenTextFileWriter 方法的工作範例。

收件者 請參閱
使用將文字寫入檔案 StreamWriter 作法:以 StreamWriter 將文字寫入檔案

另請參閱

適用於

OpenTextFileWriter(String, Boolean, Encoding)

開啟 StreamWriter,以便寫入指定的檔案。

public:
 System::IO::StreamWriter ^ OpenTextFileWriter(System::String ^ file, bool append, System::Text::Encoding ^ encoding);
public System.IO.StreamWriter OpenTextFileWriter (string file, bool append, System.Text.Encoding encoding);
member this.OpenTextFileWriter : string * bool * System.Text.Encoding -> System.IO.StreamWriter
Public Function OpenTextFileWriter (file As String, append As Boolean, encoding As Encoding) As StreamWriter

參數

file
String

要寫入的檔案。

append
Boolean

True 表示要附加至檔案內容,False 表示要覆寫檔案內容。 預設為 False

encoding
Encoding

寫入檔案時使用的編碼方式。 預設為 ASCII。

傳回

要寫入到指定檔案的 StreamWriter 物件。

例外狀況

檔案名稱以斜線結束。

範例

這個範例會使用 方法開啟 StreamWriter ,並用它來使用 類別的方法StreamWriter,將字串寫入文本檔WriteLineMy.Computer.FileSystem.OpenTextFileWriter

Dim file = My.Computer.FileSystem.OpenTextFileWriter(
    "c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()

備註

方法 OpenTextFileWriter 會開啟並初始化檔案的數據流,然後傳回 StreamWriter 該數據流的物件。 您可以視需要多次寫入至數據流,然後在完成時將其關閉。

注意

您必須在物件上StreamWriter呼叫 Close 方法,以確保所有數據都正確寫入基礎數據流。

如果您只將幾個字串寫入檔案中,使用 WriteAllText 方法可能會比較簡單。

下表列出涉及 My.Computer.FileSystem.OpenTextFileWriter 方法的工作範例。

收件者 請參閱
使用將文字寫入檔案 StreamWriter 作法:以 StreamWriter 將文字寫入檔案

另請參閱

適用於