Export (0) Print
Expand All

StreamWriter Constructor (String, Boolean, Encoding, Int32)

Initializes a new instance of the StreamWriter class for the 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.

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

new : 
        path:string * 
        append:bool * 
        encoding:Encoding * 
        bufferSize:int -> StreamWriter


Type: System.String
The complete file path to write to.
Type: System.Boolean
Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.
Type: System.Text.Encoding
The character encoding to use.
Type: System.Int32
Sets the buffer size.


path is an empty string ("").


path contains the name of a system device (com1, com2, and so on).


path or encoding is a null reference (Nothing in Visual Basic).


bufferSize is negative.


path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.


The caller does not have the required permission.


Access is denied.


The specified path is invalid, such as being on an unmapped drive.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

This constructor initializes the Encoding property using the encoding parameter. For additional information, see Encoding.

path can be a file name, including a file on a Universal Naming Convention (UNC) share.

path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.

Caution noteCaution

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

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

The following example demonstrates the StreamWriter constructors.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2014 Microsoft