TextWriter Class

Definition

Represents a writer that can write a sequential series of characters. This class is abstract.

[System.Runtime.InteropServices.ComVisible(true)]
public abstract class TextWriter : MarshalByRefObject, IDisposable
Inheritance
Derived
System.CodeDom.Compiler.IndentedTextWriter
System.IO.StreamWriter
System.IO.StringWriter
System.Web.HttpWriter
System.Web.UI.HtmlTextWriter
Attributes
Implements

Inherited Members

System.MarshalByRefObject

System.Object

Examples

The TextWriter class is an abstract class. Therefore, you do not instantiate it in your code. The StreamWriter class derives from TextWriter and provides implementations of the members for writing to a stream. The following example shows how to write two lines that consist of string values to a text file by using the WriteLineAsync(String) method.

Override Dispose in a derived class to provide

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            using (StreamWriter writer = File.CreateText("newfile.txt"))
            {
                await writer.WriteLineAsync("First line of example");
                await writer.WriteLineAsync("and second line");
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Using writer As StreamWriter = File.CreateText("newfile.txt")
            Await writer.WriteLineAsync("First line of example")
            Await writer.WriteLineAsync("and second line")
        End Using
    End Sub
End Module

Remarks

TextWriter is the abstract base class of StreamWriter and StringWriter, which write characters to streams and strings, respectively. Create an instance of TextWriter to write an object to a string, write strings to a file, or to serialize XML. You can also use an instance of TextWriter to write text to a custom backing store using the same APIs you would use for a string or a stream, or to add support for text formatting.

All the Write methods of TextWriter having primitive data types as parameters write out the values as strings.

By default, a TextWriter is not thread safe. See TextWriter.Synchronized for a thread-safe wrapper.

Important

This type implements the IDisposable interface. When you have finished using any type that derives from this type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see Dispose and the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

For a list of common I/O tasks, see Common I/O Tasks.

Constructors

TextWriter()

Initializes a new instance of the TextWriter class.

TextWriter(IFormatProvider)

Initializes a new instance of the TextWriter class with the specified format provider.

Fields

CoreNewLine

Stores the newline characters used for this TextWriter.

Null

Provides a TextWriter with no backing store that can be written to, but not read from.

Properties

Encoding

When overridden in a derived class, returns the character encoding in which the output is written.

FormatProvider

Gets an object that controls formatting.

NewLine

Gets or sets the line terminator string used by the current TextWriter.

Methods

Close()

Closes the current writer and releases any system resources associated with the writer.

Dispose()

Releases all resources used by the TextWriter object.

Dispose(Boolean)

Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.

Flush()

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

FlushAsync()

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

Synchronized(TextWriter)

Creates a thread-safe wrapper around the specified TextWriter.

Write(String, Object, Object, Object)

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object, Object, Object) method.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the text string or stream.

Write(String, Object[])

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object[]) method.

Write(String, Object)

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object) method.

Write(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text string or stream.

Write(UInt32)

Writes the text representation of a 4-byte unsigned integer to the text string or stream.

Write(String)

Writes a string to the text string or stream.

Write(Single)

Writes the text representation of a 4-byte floating-point value to the text string or stream.

Write(String, Object, Object)

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object, Object) method.

Write(Int64)

Writes the text representation of an 8-byte signed integer to the text string or stream.

Write(Int32)

Writes the text representation of a 4-byte signed integer to the text string or stream.

Write(Double)

Writes the text representation of an 8-byte floating-point value to the text string or stream.

Write(Decimal)

Writes the text representation of a decimal value to the text string or stream.

Write(Char[])

Writes a character array to the text string or stream.

Write(Char)

Writes a character to the text string or stream.

Write(Object)

Writes the text representation of an object to the text string or stream by calling the ToString method on that object.

Write(Boolean)

Writes the text representation of a Boolean value to the text string or stream.

WriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the text string or stream asynchronously.

WriteAsync(String)

Writes a string to the text string or stream asynchronously.

WriteAsync(Char)

Writes a character to the text string or stream asynchronously.

WriteAsync(Char[])

Writes a character array to the text string or stream asynchronously.

WriteLine(String)

Writes a string followed by a line terminator to the text string or stream.

WriteLine(String, Object, Object)

Writes a formatted string and a new line to the text string or stream, using the same semantics as the Format(String, Object, Object) method.

WriteLine(Char[], Int32, Int32)

Writes a subarray of characters followed by a line terminator to the text string or stream.

WriteLine(String, Object[])

Writes out a formatted string and a new line, using the same semantics as Format(String, Object).

WriteLine(String, Object)

Writes a formatted string and a new line to the text string or stream, using the same semantics as the Format(String, Object) method.

WriteLine(UInt64)

Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text string or stream.

WriteLine(String, Object, Object, Object)

Writes out a formatted string and a new line, using the same semantics as Format(String, Object).

WriteLine(UInt32)

Writes the text representation of a 4-byte unsigned integer followed by a line terminator to the text string or stream.

WriteLine(Single)

Writes the text representation of a 4-byte floating-point value followed by a line terminator to the text string or stream.

WriteLine(Double)

Writes the text representation of a 8-byte floating-point value followed by a line terminator to the text string or stream.

WriteLine(Int64)

Writes the text representation of an 8-byte signed integer followed by a line terminator to the text string or stream.

WriteLine(Int32)

Writes the text representation of a 4-byte signed integer followed by a line terminator to the text string or stream.

WriteLine(Decimal)

Writes the text representation of a decimal value followed by a line terminator to the text string or stream.

WriteLine(Char[])

Writes an array of characters followed by a line terminator to the text string or stream.

WriteLine(Char)

Writes a character followed by a line terminator to the text string or stream.

WriteLine(Boolean)

Writes the text representation of a Boolean value followed by a line terminator to the text string or stream.

WriteLine()

Writes a line terminator to the text string or stream.

WriteLine(Object)

Writes the text representation of an object by calling the ToString method on that object, followed by a line terminator to the text string or stream.

WriteLineAsync(String)

Writes a string followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync()

Writes a line terminator asynchronously to the text string or stream.

WriteLineAsync(Char)

Writes a character followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync(Char[])

Writes an array of characters followed by a line terminator asynchronously to the text string or stream.

WriteLineAsync(Char[], Int32, Int32)

Writes a subarray of characters followed by a line terminator asynchronously to the text string or stream.