StringWriter 建構函式

定義

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

多載

StringWriter()

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

StringWriter(IFormatProvider)

使用指定的格式控制項,初始化 StringWriter 類別的新執行個體。

StringWriter(StringBuilder)

初始化 StringWriter 類別的新執行個體,這個執行個體可將資源寫入指定的 StringBuilder

StringWriter(StringBuilder, IFormatProvider)

初始化 StringWriter 類別的新執行個體,這會寫入指定的 StringBuilder,且具有指定的格式提供者。

StringWriter()

來源:
StringWriter.cs
來源:
StringWriter.cs
來源:
StringWriter.cs

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

public:
 StringWriter();
public StringWriter ();
Public Sub New ()

範例

下列程式代碼範例示範如何使用 類別建構字串 StringWriter

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   StringWriter^ strWriter = gcnew StringWriter;
   
   // Use the three overloads of the Write method that are 
   // overridden by the StringWriter class.
   strWriter->Write( "file path characters are: " );
   strWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length );
   strWriter->Write( Char::Parse( "." ) );
   
   // Use the underlying StringBuilder for more complex 
   // manipulations of the string.
   strWriter->GetStringBuilder()->Insert( 0, "Invalid " );
   
   Console::WriteLine( "The following string is {0} encoded.\n{1}", strWriter->Encoding->EncodingName, strWriter->ToString() );
   
}
using System;
using System.IO;
using System.Text;

class StrWriter
{
    static void Main()
    {
        StringWriter strWriter  = new StringWriter();

        // Use the three overloads of the Write method that are
        // overridden by the StringWriter class.
        strWriter.Write("file path characters are: ");
        strWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);
        strWriter.Write('.');

        // Use the underlying StringBuilder for more complex
        // manipulations of the string.
        strWriter.GetStringBuilder().Insert(0, "Invalid ");

        Console.WriteLine("The following string is {0} encoded.\n{1}",
            strWriter.Encoding.EncodingName, strWriter.ToString());
    }
}
Imports System.IO
Imports System.Text

Public Class StrWriter

    Shared Sub Main()

        Dim strWriter As StringWriter = new StringWriter()

        ' Use the three overloads of the Write method that are 
        ' overridden by the StringWriter class.
        strWriter.Write("file path characters are: ")
        strWriter.Write( _
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)
        strWriter.Write("."C)

        ' Use the underlying StringBuilder for more complex 
        ' manipulations of the string.
        strWriter.GetStringBuilder().Insert(0, "Invalid ")

        Console.WriteLine("The following string is {0} encoded." _
            & vbCrLf & "{1}", _
            strWriter.Encoding.EncodingName, strWriter.ToString())

    End Sub
End Class

備註

系統會自動建立新的 StringBuilder 物件,並與類別的新實例 StringWriter 相關聯。 由於未為此建構函式指定格式控件,因此會使用 CultureInfo.CurrentCulture初始化新的實例。

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringWriter(IFormatProvider)

來源:
StringWriter.cs
來源:
StringWriter.cs
來源:
StringWriter.cs

使用指定的格式控制項,初始化 StringWriter 類別的新執行個體。

public:
 StringWriter(IFormatProvider ^ formatProvider);
public StringWriter (IFormatProvider formatProvider);
public StringWriter (IFormatProvider? formatProvider);
new System.IO.StringWriter : IFormatProvider -> System.IO.StringWriter
Public Sub New (formatProvider As IFormatProvider)

參數

formatProvider
IFormatProvider

控制格式設定的 IFormatProvider 物件。

範例

下列程式代碼範例示範如何建構特定文化特性中的字串。

using namespace System;
using namespace System::Globalization;
using namespace System::IO;
int main()
{
   StringWriter^ strWriter = gcnew StringWriter( gcnew CultureInfo(  "ar-DZ" ) );
   strWriter->Write( DateTime::Now );
   
   Console::WriteLine( "Current date and time using the invariant culture: {0}\n"
   "Current date and time using the Algerian culture: {1}", DateTime::Now.ToString(), strWriter->ToString() );
   
}
using System;
using System.Globalization;
using System.IO;

class StrWriter
{
    static void Main()
    {
        StringWriter strWriter =
            new StringWriter(new CultureInfo("ar-DZ"));

        strWriter.Write(DateTime.Now);

        Console.WriteLine(
            "Current date and time using the invariant culture: {0}\n" +
            "Current date and time using the Algerian culture: {1}",
            DateTime.Now.ToString(), strWriter.ToString());
    }
}
Imports System.Globalization
Imports System.IO

Public Class StrWriter

    Shared Sub Main()
        Dim strWriter As New StringWriter(New CultureInfo("ar-DZ"))

        strWriter.Write(DateTime.Now)

        Console.WriteLine( _
            "Current date and time using the invariant culture: {0}" _
            & vbCrLf & _
            "Current date and time using the Algerian culture: {1}", _
            DateTime.Now.ToString(), strWriter.ToString())
    End Sub

End Class

備註

系統會自動建立新的 StringBuilder 物件,並與類別的新實例 StringWriter 相關聯。

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringWriter(StringBuilder)

來源:
StringWriter.cs
來源:
StringWriter.cs
來源:
StringWriter.cs

初始化 StringWriter 類別的新執行個體,這個執行個體可將資源寫入指定的 StringBuilder

public:
 StringWriter(System::Text::StringBuilder ^ sb);
public StringWriter (System.Text.StringBuilder sb);
new System.IO.StringWriter : System.Text.StringBuilder -> System.IO.StringWriter
Public Sub New (sb As StringBuilder)

參數

sb
StringBuilder

要寫入至其中的 StringBuilder 物件。

例外狀況

sbnull

範例

下列程式代碼範例示範如何使用 StringBuilder 類別來修改已關閉 StringWriter中的基礎字串。

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   StringBuilder^ strBuilder = gcnew StringBuilder( "file path characters are: " );
   StringWriter^ strWriter = gcnew StringWriter( strBuilder );
   strWriter->Write( Path::InvalidPathChars, 0, Path::InvalidPathChars->Length );
   
   strWriter->Close();
   
   // Since the StringWriter is closed, an exception will 
   // be thrown if the Write method is called. However, 
   // the StringBuilder can still manipulate the string.
   strBuilder->Insert( 0, "Invalid " );
   Console::WriteLine( strWriter->ToString() );
   
}
using System;
using System.IO;
using System.Text;

class StrWriter
{
    static void Main()
    {
        StringBuilder strBuilder =
            new StringBuilder("file path characters are: ");
        StringWriter strWriter = new StringWriter(strBuilder);

        strWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        strWriter.Close();

        // Since the StringWriter is closed, an exception will
        // be thrown if the Write method is called. However,
        // the StringBuilder can still manipulate the string.
        strBuilder.Insert(0, "Invalid ");
        Console.WriteLine(strWriter.ToString());
    }
}
Imports System.IO
Imports System.Text

Public Class StrWriter

    Shared Sub Main()
        Dim strBuilder As New StringBuilder( _
            "file path characters are: ")
        Dim strWriter As New StringWriter(strBuilder)

        strWriter.Write( _
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length)

        strWriter.Close()

        ' Since the StringWriter is closed, an exception will 
        ' be thrown if the Write method is called. However, 
        ' the StringBuilder can still manipulate the string.
        strBuilder.Insert(0, "Invalid ")
        Console.WriteLine(strWriter.ToString())
    End Sub

End Class

備註

由於未為此建構函式指定格式控件,因此會使用 CultureInfo.CurrentCulture初始化新的實例。

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringWriter(StringBuilder, IFormatProvider)

來源:
StringWriter.cs
來源:
StringWriter.cs
來源:
StringWriter.cs

初始化 StringWriter 類別的新執行個體,這會寫入指定的 StringBuilder,且具有指定的格式提供者。

public:
 StringWriter(System::Text::StringBuilder ^ sb, IFormatProvider ^ formatProvider);
public StringWriter (System.Text.StringBuilder sb, IFormatProvider formatProvider);
public StringWriter (System.Text.StringBuilder sb, IFormatProvider? formatProvider);
new System.IO.StringWriter : System.Text.StringBuilder * IFormatProvider -> System.IO.StringWriter
Public Sub New (sb As StringBuilder, formatProvider As IFormatProvider)

參數

sb
StringBuilder

要寫入至其中的 StringBuilder 物件。

formatProvider
IFormatProvider

控制格式設定的 IFormatProvider 物件。

例外狀況

sbnull

備註

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔。 作法:讀取和寫入新建立的資料檔案

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0