StringWriter Constructor (StringBuilder)


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

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public StringWriter(
	StringBuilder sb


Type: System.Text.StringBuilder

The StringBuilder object to write to.

Exception Condition

sb is null.

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



Get the size of a file.


Get the attributes of a file.


Set the attributes of a file.


Determine if a 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

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

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);

            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);


        // 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 ");

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top