Прочетете на английски Редактиране

Споделяне чрез


StringWriter Constructors

Definition

Initializes a new instance of the StringWriter class.

Overloads

StringWriter()

Initializes a new instance of the StringWriter class.

StringWriter(IFormatProvider)

Initializes a new instance of the StringWriter class with the specified format control.

StringWriter(StringBuilder)

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.

StringWriter(StringBuilder, IFormatProvider)

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

StringWriter()

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class.

C#
public StringWriter();

Examples

The following code example demonstrates how to construct a string using the StringWriter class.

C#
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());
    }
}

Remarks

A new StringBuilder object is automatically created and associated with the new instance of the StringWriter class. Since a format control is not specified for this constructor, the new instance will be initialized with CultureInfo.CurrentCulture.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. How to: Write Text to a File
Write to a text file. How to: Write Text to a File
Read from a text file. How to: Read Text from a File
Append text to a file. How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText
Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. How to: Read and Write to a Newly Created Data File
Write to a binary file. How to: Read and Write to a Newly Created Data File

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class with the specified format control.

C#
public StringWriter(IFormatProvider formatProvider);
C#
public StringWriter(IFormatProvider? formatProvider);

Parameters

formatProvider
IFormatProvider

An IFormatProvider object that controls formatting.

Examples

The following code example demonstrates how to construct a string in a specific culture.

C#
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());
    }
}

Remarks

A new StringBuilder object is automatically created and associated with the new instance of the StringWriter class.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. How to: Write Text to a File
Write to a text file. How to: Write Text to a File
Read from a text file. How to: Read Text from a File
Append text to a file. How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText
Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. How to: Read and Write to a Newly Created Data File
Write to a binary file. How to: Read and Write to a Newly Created Data File

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.

C#
public StringWriter(System.Text.StringBuilder sb);

Parameters

sb
StringBuilder

The StringBuilder object to write to.

Exceptions

sb is null.

Examples

The following code example demonstrates using the StringBuilder class to modify the underlying string in a closed StringWriter.

C#
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());
    }
}

Remarks

Since a format control is not specified for this constructor, the new instance will be initialized with CultureInfo.CurrentCulture.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. How to: Write Text to a File
Write to a text file. How to: Write Text to a File
Read from a text file. How to: Read Text from a File
Append text to a file. How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText
Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. How to: Read and Write to a Newly Created Data File
Write to a binary file. How to: Read and Write to a Newly Created Data File

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

C#
public StringWriter(System.Text.StringBuilder sb, IFormatProvider formatProvider);
C#
public StringWriter(System.Text.StringBuilder sb, IFormatProvider? formatProvider);

Parameters

sb
StringBuilder

The StringBuilder object to write to.

formatProvider
IFormatProvider

An IFormatProvider object that controls formatting.

Exceptions

sb is null.

Remarks

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. How to: Write Text to a File
Write to a text file. How to: Write Text to a File
Read from a text file. How to: Read Text from a File
Append text to a file. How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText
Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. How to: Read and Write to a Newly Created Data File
Write to a binary file. How to: Read and Write to a Newly Created Data File

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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