Professional Documents
Culture Documents
StreamWriter Class
Namespace: System.IO
Assembly: System.Runtime.dll
Constructors
Fields
Properties
Methods
Explicit Interface Implementations
Extension Methods
Applies to
See also
C# = Copy
Examples
The following example shows how to use a StreamWriter object to write a file that lists the
directories on the C drive, and then uses a StreamReader object to read and display each
directory name. A good practice is to use these objects in a using statement so that the
unmanaged resources are correctly disposed. The using statement automatically calls
Dispose on the object when the code that is using it has completed. The constructor used
in this example is not supported for use in Windows Store Apps.
= Copy
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StreamReadWrite
{
class Program
{
static void Main(string[] args)
{
// Get the directories currently on the C drive.
DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();
Remarks
StreamWriter is designed for character output in a particular encoding, whereas classes
derived from Stream are designed for byte input and output.
) Important
This type implements the IDisposable interface. When you have finished using the
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 the "Using an Object that Implements IDisposable" section in the
IDisposable interface topic.
Constructors
StreamWriter(Stream) Initializes a new instance of the StreamWriter class for the
specified stream by using UTF-8 encoding and the default buffer
size.
StreamWriter(String, Boolean) Initializes a new instance of the StreamWriter class for the
specified file by using the default encoding and buffer size. If the
file exists, it can be either overwritten or appended to. If the file
does not exist, this constructor creates a new file.
StreamWriter(String, Boolean, Initializes a new instance of the StreamWriter class for the
Encoding) specified file by using the specified encoding and default buffer
size. If the file exists, it can be either overwritten or appended to.
If the file does not exist, this constructor creates a new file.
StreamWriter(String, Boolean, Initializes a new instance of the StreamWriter class for the
Encoding, Int32) specified file on the specified path, using the specified encoding
and buffer size. If the file exists, it can be either overwritten or
appended to. If the file does not exist, this constructor creates a
new file.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 4/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
Fields
CoreNewLine Stores the newline characters used for this TextWriter .
(Inherited from TextWriter)
Properties
AutoFlush Gets or sets a value indicating whether the StreamWriter will
flush its buffer to the underlying stream after every call to
Write(Char).
BaseStream Gets the underlying stream that interfaces with a backing store.
NewLine Gets or sets the line terminator string used by the current
TextWriter .
(Inherited from TextWriter)
Methods
Close() Closes the current StreamWriter object and the underlying
stream.
Flush() Clears all buffers for the current writer and causes any buffered
data to be written to the underlying stream.
FlushAsync() Clears all buffers for this stream asynchronously and causes any
buffered data to be written to the underlying device.
GetLifetimeService() Obsolete.
Retrieves the current lifetime service object that controls the
lifetime policy for this instance.
(Inherited from MarshalByRefObject)
InitializeLifetimeService() Obsolete.
Write(String, Object) Writes a formatted string to the stream, using the same
semantics as the Format(String, Object) method.
Write(String, Object, Object) Writes a formatted string to the stream using the same
semantics as the Format(String, Object, Object) method..
Write(String, Object, Object, Writes a formatted string to the stream, using the same
Object) semantics as the Format(String Object Object Object) method
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 7/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
Object) semantics as the Format(String, Object, Object, Object) method.
Write(String, Object[]) Writes a formatted string to the stream, using the same
semantics as the Format(String, Object[]) method.
WriteLine(Char[], Int32, Int32) Writes a subarray of characters to the text stream, followed by a
line terminator.
(Inherited from TextWriter)
WriteLine(String, Object) Writes a formatted string and a new line to the stream, using the
same semantics as the Format(String, Object) method.
WriteLine(String, Object, Writes a formatted string and a new line to the stream, using the
Object) same semantics as the Format(String, Object, Object) method.
WriteLine(String, Object, Writes out a formatted string and a new line to the stream, using
Object Object) the same semantics as Format(String Object)
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 9/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
Object, Object) the same semantics as Format(String, Object).
WriteLine(String, Object[]) Writes out a formatted string and a new line to the stream, using
the same semantics as Format(String, Object).
Extension Methods
ConfigureAwait(IAsync Configures how awaits on the tasks returned from an async
Disposable, Boolean) disposable are performed.
Applies to
Product Versions
.NET 5.0
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0
Xamarin.Android 7.1
Xamarin.iOS 10.8
Product Versions
Xamarin.Mac 3.0
See also
Stream
StreamReader
Encoder
Encoding
File and Stream I/O
How to: Read Text from a File
How to: Write Text to a File
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 11/11