StreamWriter StreamWriter StreamWriter StreamWriter Constructors

Definition

Overloads

StreamWriter(Stream) StreamWriter(Stream) 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) StreamWriter(String) StreamWriter(String) StreamWriter(String)

Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size.

StreamWriter(Stream, Encoding) StreamWriter(Stream, Encoding) StreamWriter(Stream, Encoding)

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and the default buffer size.

StreamWriter(String, Boolean) StreamWriter(String, Boolean) StreamWriter(String, Boolean) 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(Stream, Encoding, Int32) StreamWriter(Stream, Encoding, Int32) StreamWriter(Stream, Encoding, Int32)

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size.

StreamWriter(String, Boolean, Encoding) StreamWriter(String, Boolean, Encoding) StreamWriter(String, Boolean, Encoding)

Initializes a new instance of the StreamWriter class for the 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(Stream, Encoding, Int32, Boolean) StreamWriter(Stream, Encoding, Int32, Boolean) StreamWriter(Stream, Encoding, Int32, Boolean)

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size, and optionally leaves the stream open.

StreamWriter(String, Boolean, Encoding, Int32) StreamWriter(String, Boolean, Encoding, Int32) StreamWriter(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.

StreamWriter(Stream) StreamWriter(Stream) StreamWriter(Stream)

Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

public:
 StreamWriter(System::IO::Stream ^ stream);
public StreamWriter (System.IO.Stream stream);
new System.IO.StreamWriter : System.IO.Stream -> System.IO.StreamWriter
Parameters
stream
Stream Stream Stream Stream

The stream to write to.

Exceptions

Examples

The following code example demonstrates this constructor.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

Remarks

This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. To specify whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter. The BaseStream property is initialized using the stream parameter. The position of the stream is not reset.

The StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

Caution

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.

StreamWriter(String) StreamWriter(String) StreamWriter(String) StreamWriter(String)

Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size.

public:
 StreamWriter(System::String ^ path);
public StreamWriter (string path);
new System.IO.StreamWriter : string -> System.IO.StreamWriter
Public Sub New (path As String)
Parameters
path
String String String String

The complete file path to write to. path can be a file name.

Exceptions

path is an empty string ("").

-or-

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

The specified path is invalid (for example, it is on an unmapped drive).

The specified path, file name, or both exceed the system-defined maximum length.

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

The caller does not have the required permission.

Examples

The following code example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

Remarks

This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding).

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share. If the file exists, it is overwritten; otherwise, a new file is created.

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

Caution

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.

Security

FileIOPermission
for reading and writing files. Associated enumeration: Read, Write

StreamWriter(Stream, Encoding) StreamWriter(Stream, Encoding) StreamWriter(Stream, Encoding)

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and the default buffer size.

public:
 StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding -> System.IO.StreamWriter
Parameters
stream
Stream Stream Stream Stream

The stream to write to.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

Exceptions

Examples

The following example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
               fs = new FileStream(fileName, FileMode.CreateNew);
               using (StreamWriter writer = new StreamWriter(fs, Encoding.Default))
                {
                    writer.Write(textToAdd);
                }
            }       
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

Remarks

This constructor initializes the Encoding property using the encoding parameter, and the BaseStream property using the stream parameter. The position of the stream is not reset. For additional information, see Encoding.

The StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

Caution

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.

See Also

StreamWriter(String, Boolean) StreamWriter(String, Boolean) StreamWriter(String, Boolean) 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.

public:
 StreamWriter(System::String ^ path, bool append);
public StreamWriter (string path, bool append);
new System.IO.StreamWriter : string * bool -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean)
Parameters
path
String String String String

The complete file path to write to.

append
Boolean Boolean Boolean Boolean

true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

Exceptions

path is empty.

-or-

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

The specified path is invalid (for example, it is on an unmapped drive).

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

The specified path, file name, or both exceed the system-defined maximum length.

The caller does not have the required permission.

Examples

The following code example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            
            using (StreamWriter writer = new StreamWriter(fileName, true))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName, True)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

Remarks

This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding).

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

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

Caution

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.

Security

FileIOPermission
for reading and writing files. Associated enumeration: Read, Write

StreamWriter(Stream, Encoding, Int32) StreamWriter(Stream, Encoding, Int32) StreamWriter(Stream, Encoding, Int32)

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size.

public:
 StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int -> System.IO.StreamWriter
Parameters
stream
Stream Stream Stream Stream

The stream to write to.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

bufferSize
Int32 Int32 Int32 Int32

The buffer size, in bytes.

Exceptions

Examples

The following example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

Remarks

This constructor initializes the Encoding property using the encoding parameter, and the BaseStream property using the stream parameter. The position of the stream is not reset. For additional information, see Encoding.

The StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

Caution

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.

See Also

StreamWriter(String, Boolean, Encoding) StreamWriter(String, Boolean, Encoding) StreamWriter(String, Boolean, Encoding)

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

public:
 StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding);
public StreamWriter (string path, bool append, System.Text.Encoding encoding);
new System.IO.StreamWriter : string * bool * System.Text.Encoding -> System.IO.StreamWriter
Parameters
path
String String String String

The complete file path to write to.

append
Boolean Boolean Boolean Boolean

true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

Exceptions

path is empty.

-or-

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

The specified path is invalid (for example, it is on an unmapped drive).

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

The specified path, file name, or both exceed the system-defined maximum length.

The caller does not have the required permission.

Examples

The following example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            
            using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

Remarks

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

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.

Security

FileIOPermission
for reading and writing files. Associated enumeration: Read, Write

See Also

StreamWriter(Stream, Encoding, Int32, Boolean) StreamWriter(Stream, Encoding, Int32, Boolean) StreamWriter(Stream, Encoding, Int32, Boolean)

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size, and optionally leaves the stream open.

public:
 StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize, bool leaveOpen);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int * bool -> System.IO.StreamWriter
Parameters
stream
Stream Stream Stream Stream

The stream to write to.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

bufferSize
Int32 Int32 Int32 Int32

The buffer size, in bytes.

leaveOpen
Boolean Boolean Boolean Boolean

true to leave the stream open after the StreamWriter object is disposed; otherwise, false.

Exceptions

Examples

The following example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.CreateNew);
                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512, false))
                {
                    writer.Write(textToAdd);
                }
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"
        Dim fs As FileStream = Nothing
        Try
            fs = New FileStream(fileName, FileMode.CreateNew)
            Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512, False)
                writer.Write(textToAdd)
            End Using
        Finally
            If Not fs Is Nothing Then
                fs.Dispose()
            End If
        End Try
    End Sub

End Module

Remarks

Unless you set the leaveOpen parameter to true, the StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

This constructor initializes the Encoding property by using the encoding parameter, and initializes the BaseStream property by using the stream parameter. The position of the stream is not reset. For additional information, see the Encoding property.

Caution

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.

StreamWriter(String, Boolean, Encoding, Int32) StreamWriter(String, Boolean, Encoding, Int32) StreamWriter(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.

public:
 StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (string path, bool append, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : string * bool * System.Text.Encoding * int -> System.IO.StreamWriter
Parameters
path
String String String String

The complete file path to write to.

append
Boolean Boolean Boolean Boolean

true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

bufferSize
Int32 Int32 Int32 Int32

The buffer size, in bytes.

Exceptions

path is an empty string ("").

-or-

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

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

The caller does not have the required permission.

The specified path is invalid (for example, it is on an unmapped drive).

The specified path, file name, or both exceed the system-defined maximum length.

Examples

The following example demonstrates this constructor.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            
            using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8, 512))
            {
                writer.Write(textToAdd);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileName As String = "test.txt"
        Dim textToAdd As String = "Example text in file"

        Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8, 512)
            writer.Write(textToAdd)
        End Using
    End Sub

End Module

Remarks

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

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.

Security

FileIOPermission
for reading and writing files. Associated enumeration: Read, Write

See Also

Applies to