StreamWriter 建構函式

定義

初始化 StreamWriter 類別的新執行個體。

多載

StreamWriter(Stream)

使用 UTF-8 編碼方式和預設緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體。

StreamWriter(String)

使用預設編碼方式和緩衝區大小,為指定的檔案初始化 StreamWriter 類別的新執行個體。

StreamWriter(Stream, Encoding)

使用指定的編碼方式和預設緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體。

StreamWriter(String, Boolean)

使用預設編碼方式和緩衝區大小,為指定的檔案初始化 StreamWriter 類別的新執行個體。 如果檔案存在,可以將它寫入或附加。 如果檔案不存在,這個建構函式會建立新的檔案。

StreamWriter(String, FileStreamOptions)

使用預設編碼方式,並使用指定的 FileStreamOptions 物件設定,初始化 指定檔案之 類別的新實例StreamWriter

StreamWriter(Stream, Encoding, Int32)

使用指定的編碼方式和緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體。

StreamWriter(String, Boolean, Encoding)

使用指定的編碼方式和預設緩衝區大小,為指定的檔案初始化 StreamWriter 類別的新執行個體。 如果檔案存在,可以將它寫入或附加。 如果檔案不存在,這個建構函式會建立新的檔案。

StreamWriter(String, Encoding, FileStreamOptions)

使用指定的編碼方式,並使用FileStreamOptions指定的物件設定,初始化 指定檔案之 類別的新實例StreamWriter

StreamWriter(Stream, Encoding, Int32, Boolean)

使用指定的編碼方式和緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體,並選擇性讓資料流保持開啟。

StreamWriter(String, Boolean, Encoding, Int32)

使用預設編碼方式和緩衝區大小,為指定路徑上的指定檔案初始化 StreamWriter 類別的新執行個體。 如果檔案存在,可以將它寫入或附加。 如果檔案不存在,這個建構函式會建立新的檔案。

StreamWriter(Stream)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用 UTF-8 編碼方式和預設緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體。

public:
 StreamWriter(System::IO::Stream ^ stream);
public StreamWriter (System.IO.Stream stream);
new System.IO.StreamWriter : System.IO.Stream -> System.IO.StreamWriter
Public Sub New (stream As Stream)

參數

stream
Stream

要寫入的資料流。

例外狀況

stream 不可寫入。

streamnull

範例

下列程式代碼範例示範此建構函式。

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

備註

此建構函式會 StreamWriter 使用UTF-8編碼來建立 ,而不 Byte-Order Mark (BOM) ,因此其 GetPreamble 方法會傳回空的位元組陣列。 這個建構函式的預設UTF-8編碼會擲回無效位元組的例外狀況。 此行為與 屬性中的 Encoding.UTF8 編碼物件所提供的行為不同。 若要指定是否在無效的位元組上擲回例外狀況,請使用接受編碼物件的建構函式做為參數,例如 StreamWriter。 屬性 BaseStream 是使用 stream 參數初始化。 數據流的位置不會重設。

呼叫 時,物件StreamWriter會在提供Stream的物件上呼叫Dispose()StreamWriter.Dispose

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

StreamWriter(String)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用預設編碼方式和緩衝區大小,為指定的檔案初始化 StreamWriter 類別的新執行個體。

public:
 StreamWriter(System::String ^ path);
public StreamWriter (string path);
new System.IO.StreamWriter : string -> System.IO.StreamWriter
Public Sub New (path As String)

參數

path
String

要寫入的完整檔案路徑。 path 可以是檔案名稱。

例外狀況

存取遭到拒絕。

path 為空字串 ("")。

-或-

path 包含系統裝置的名稱 (com1、com2 等等)。

pathnull

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

path 包含檔案名稱、目錄名稱或磁碟標籤語法的不正確或無效語法。

呼叫端沒有必要的權限。

範例

下列程式代碼範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

備註

此建構函式會 StreamWriter 使用UTF-8編碼來建立 ,而不 Byte-Order Mark (BOM) ,因此其 GetPreamble 方法會傳回空的位元組陣列。 這個建構函式的預設UTF-8編碼會擲回無效位元組的例外狀況。 此行為與 屬性中的 Encoding.UTF8 編碼物件所提供的行為不同。 若要指定 BOM 並判斷是否在無效的位元組上擲回例外狀況,請使用接受編碼物件的建構函式做為參數,例如 StreamWriter(String, Boolean, Encoding)

參數 path 可以是檔名,包括通用命名慣例上的檔案, (UNC) 共用。 如果檔案存在,則會覆寫它;否則會建立新的檔案。

path參數不需要是儲存在磁碟上的檔案;它可以是支援使用數據流存取之系統的任何部分。

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

StreamWriter(Stream, Encoding)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用指定的編碼方式和預設緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體。

public:
 StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding)

參數

stream
Stream

要寫入的資料流。

encoding
Encoding

要使用的字元編碼。

例外狀況

streamencodingnull

stream 不可寫入。

範例

下列範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
               fs = new FileStream(fileName, FileMode.CreateNew);
               using (StreamWriter writer = new StreamWriter(fs, Encoding.Default))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

備註

這個建構函式會使用編碼參數初始化 Encoding 屬性,並使用 BaseStream 數據流參數初始化 屬性。 數據流的位置不會重設。 如需詳細資訊,請參閱 Encoding

呼叫 時,物件StreamWriter會在提供Stream的物件上呼叫Dispose()StreamWriter.Dispose

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

StreamWriter(String, Boolean)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用預設編碼方式和緩衝區大小,為指定的檔案初始化 StreamWriter 類別的新執行個體。 如果檔案存在,可以將它寫入或附加。 如果檔案不存在,這個建構函式會建立新的檔案。

public:
 StreamWriter(System::String ^ path, bool append);
public StreamWriter (string path, bool append);
new System.IO.StreamWriter : string * bool -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean)

參數

path
String

要寫入的完整檔案路徑。

append
Boolean

true 表示要附加資料至檔案,false 表示要覆寫檔案。 如果指定的檔案不存在,則這個參數沒有任何作用,而且建構函式會建立新的檔案。

例外狀況

存取遭到拒絕。

path 是空的。

-或-

path 包含系統裝置的名稱 (com1、com2 等等)。

pathnull

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

path 包含檔案名稱、目錄名稱或磁碟標籤語法的不正確或無效語法。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

呼叫端沒有必要的權限。

範例

下列程式代碼範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            using (StreamWriter writer = new StreamWriter(fileName, true))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName, True)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

備註

此建構函式會 StreamWriter 使用UTF-8編碼來建立 ,而不 Byte-Order Mark (BOM) ,因此其 GetPreamble 方法會傳回空的位元組陣列。 這個建構函式的預設UTF-8編碼會擲回無效位元組的例外狀況。 此行為與 屬性中的 Encoding.UTF8 編碼物件所提供的行為不同。 若要指定 BOM 並判斷是否在無效的位元組上擲回例外狀況,請使用接受編碼物件的建構函式做為參數,例如 StreamWriter(String, Boolean, Encoding)

參數 path 可以是檔名,包括通用命名慣例上的檔案, (UNC) 共用。

path參數不需要是儲存在磁碟上的檔案;它可以是支援使用數據流存取之系統的任何部分。

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

StreamWriter(String, FileStreamOptions)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用預設編碼方式,並使用指定的 FileStreamOptions 物件設定,初始化 指定檔案之 類別的新實例StreamWriter

public:
 StreamWriter(System::String ^ path, System::IO::FileStreamOptions ^ options);
public StreamWriter (string path, System.IO.FileStreamOptions options);
new System.IO.StreamWriter : string * System.IO.FileStreamOptions -> System.IO.StreamWriter
Public Sub New (path As String, options As FileStreamOptions)

參數

path
String

要寫入的完整檔案路徑。

options
FileStreamOptions

物件,指定基礎 FileStream的組態選項。

例外狀況

optionsnull

stream 不可寫入。

另請參閱

適用於

StreamWriter(Stream, Encoding, Int32)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用指定的編碼方式和緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體。

public:
 StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding, bufferSize As Integer)

參數

stream
Stream

要寫入的資料流。

encoding
Encoding

要使用的字元編碼。

bufferSize
Int32

緩衝區大小,以位元組為單位。

例外狀況

streamencodingnull

bufferSize 為負。

stream 不可寫入。

範例

下列範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

備註

這個建構函式會使用 參數初始化 Encoding 屬性,並使用 BaseStream 參數初始化 streamencoding 屬性。 數據流的位置不會重設。 如需詳細資訊,請參閱 Encoding

呼叫 時,物件StreamWriter會在提供Stream的物件上呼叫Dispose()StreamWriter.Dispose

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

StreamWriter(String, Boolean, Encoding)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用指定的編碼方式和預設緩衝區大小,為指定的檔案初始化 StreamWriter 類別的新執行個體。 如果檔案存在,可以將它寫入或附加。 如果檔案不存在,這個建構函式會建立新的檔案。

public:
 StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding);
public StreamWriter (string path, bool append, System.Text.Encoding encoding);
new System.IO.StreamWriter : string * bool * System.Text.Encoding -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean, encoding As Encoding)

參數

path
String

要寫入的完整檔案路徑。

append
Boolean

true 表示要附加資料至檔案,false 表示要覆寫檔案。 如果指定的檔案不存在,則這個參數沒有任何作用,而且建構函式會建立新的檔案。

encoding
Encoding

要使用的字元編碼。

例外狀況

存取遭到拒絕。

path 是空的。

-或-

path 包含系統裝置的名稱 (com1、com2 等等)。

pathnull

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

path 包含檔案名稱、目錄名稱或磁碟標籤語法的不正確或無效語法。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

呼叫端沒有必要的權限。

範例

下列範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

備註

這個建構函式會使用編碼參數初始化 Encoding 屬性。 如需詳細資訊,請參閱 Encoding

path 可以是檔名,包括通用命名慣例上的檔案, (UNC) 共用。

path 不需要是儲存在磁碟上的檔案;它可以是支援透過數據流存取之系統的任何部分。

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

StreamWriter(String, Encoding, FileStreamOptions)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用指定的編碼方式,並使用FileStreamOptions指定的物件設定,初始化 指定檔案之 類別的新實例StreamWriter

public:
 StreamWriter(System::String ^ path, System::Text::Encoding ^ encoding, System::IO::FileStreamOptions ^ options);
public StreamWriter (string path, System.Text.Encoding encoding, System.IO.FileStreamOptions options);
new System.IO.StreamWriter : string * System.Text.Encoding * System.IO.FileStreamOptions -> System.IO.StreamWriter
Public Sub New (path As String, encoding As Encoding, options As FileStreamOptions)

參數

path
String

要寫入的完整檔案路徑。

encoding
Encoding

要使用的字元編碼。

options
FileStreamOptions

物件,指定基礎 FileStream的組態選項。

例外狀況

optionsnull

stream 不可寫入。

另請參閱

適用於

StreamWriter(Stream, Encoding, Int32, Boolean)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用指定的編碼方式和緩衝區大小,為指定的資料流初始化 StreamWriter 類別的新執行個體,並選擇性讓資料流保持開啟。

public:
 StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding? encoding = default, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int * bool -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)

參數

stream
Stream

要寫入的資料流。

encoding
Encoding

要使用的字元編碼。

bufferSize
Int32

緩衝區大小,以位元組為單位。

leaveOpen
Boolean

true 表示在處置 StreamWriter 物件之後,將資料流保持開啟;否則為 false

例外狀況

streamencodingnull

bufferSize 為負。

stream 不可寫入。

範例

下列範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512, false))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512, False)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

備註

除非您將 leaveOpen 參數設定為 true,否則呼叫 時StreamWriter.DisposeStreamWriter物件會在提供Stream的物件上呼叫 Dispose()

這個建構函式會使用 encoding 參數初始化 Encoding 屬性,並使用 參數初始化 BaseStream 屬性stream。 數據流的位置不會重設。 如需詳細資訊,請參閱 Encoding 屬性。

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

適用於

StreamWriter(String, Boolean, Encoding, Int32)

來源:
StreamWriter.cs
來源:
StreamWriter.cs
來源:
StreamWriter.cs

使用預設編碼方式和緩衝區大小,為指定路徑上的指定檔案初始化 StreamWriter 類別的新執行個體。 如果檔案存在,可以將它寫入或附加。 如果檔案不存在,這個建構函式會建立新的檔案。

public:
 StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (string path, bool append, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : string * bool * System.Text.Encoding * int -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean, encoding As Encoding, bufferSize As Integer)

參數

path
String

要寫入的完整檔案路徑。

append
Boolean

true 表示要附加資料至檔案,false 表示要覆寫檔案。 如果指定的檔案不存在,則這個參數沒有任何作用,而且建構函式會建立新的檔案。

encoding
Encoding

要使用的字元編碼。

bufferSize
Int32

緩衝區大小,以位元組為單位。

例外狀況

path 為空字串 ("")。

-或-

path 包含系統裝置的名稱 (com1、com2 等等)。

pathencodingnull

bufferSize 為負。

path 包含檔案名稱、目錄名稱或磁碟標籤語法的不正確或無效語法。

呼叫端沒有必要的權限。

存取遭到拒絕。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

範例

下列範例示範此建構函式。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8, 512))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8, 512)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

備註

這個建構函式會使用編碼參數初始化 Encoding 屬性。 如需詳細資訊,請參閱 Encoding

path 可以是檔名,包括通用命名慣例上的檔案, (UNC) 共用。

path 不需要是儲存在磁碟上的檔案;它可以是支援透過數據流存取之系統的任何部分。

警告

當您使用特定文化設定編譯一組字元,並使用不同的文化設定擷取相同的字元時,字元可能無法解譯,而且可能會導致擲回例外狀況。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於