System.IO.StreamWriter Class

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Implements a TextWriter for writing characters to a stream in a particular encoding.
C# Syntax:
[Serializable]
public class StreamWriter : TextWriter
Remarks
StreamWriter is designed for character output in a particular Encoding, whereas classes derived from Stream are designed for byte input and output.

StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed such that the Encoding.GetPreamble method returns the Unicode byte order mark written in UTF-8. The preamble of the encoding is added to a stream when you are not appending to an existing stream. This means any text file you create with StreamWriter will have three byte order marks at its beginning. UTF-8 handles all Unicode characters correctly and gives consistent results on localized versions of the operating system.

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

See also:
System.IO Namespace | Stream | StreamReader | Encoder | Encoding

System.IO.StreamWriter Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(Stream stream)

Initializes a new instance of the StreamWriter class for the specified stream, using the default encoding and buffer size.
ctor #2 Overloaded:
.ctor(string path)

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size.
ctor #3 Overloaded:
.ctor(Stream stream, Encoding encoding)

Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding and the default buffer size.
ctor #4 Overloaded:
.ctor(string path, bool append)

Initializes a new instance of the StreamWriter class for the specified file on the specified path, 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.
ctor #5 Overloaded:
.ctor(Stream stream, Encoding encoding, int bufferSize)

Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding and buffer size.
ctor #6 Overloaded:
.ctor(string path, bool append, Encoding encoding)

Initializes a new instance of the StreamWriter class for the specified file on the specified path, 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.
ctor #7 Overloaded:
.ctor(string path, bool append, Encoding encoding, int bufferSize)

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 Fields
Null Provides a StreamWriter with no backing store that can be written to, but not read from.
Public Properties
AutoFlush Read-write

Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to Console.Write or Console.WriteLine.
BaseStream Read-only

Gets the underlying stream that interfaces with a backing store.
Encoding Read-only

Overridden:
Gets the Encoding in which the output is written.
FormatProvider
(inherited from System.IO.TextWriter)
Read-only

See base class member description: System.IO.TextWriter.FormatProvider


Gets an object that controls formatting.
NewLine
(inherited from System.IO.TextWriter)
Read-write

See base class member description: System.IO.TextWriter.NewLine


Gets or sets the line terminator string used by the current TextWriter.
Public Methods
Close Overridden:
Closes the current StreamWriter and the underlying stream.
CreateObjRef
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.CreateObjRef


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
Flush Overridden:
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.GetLifetimeService


Retrieves the current lifetime service object that controls the lifetime policy for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InitializeLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.InitializeLifetimeService


Obtains a lifetime service object to control the lifetime policy for this instance.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(bool value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of a Boolean value to the text stream.
Write Overloaded:
Write(char value)

Overridden:
Writes a character to the stream.
Write Overloaded:
Write(char[] buffer)

Overridden:
Writes a character array to the stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(decimal value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of a decimal value followed by a line terminator to the text stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(double value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of an 8-byte floating-point value to the text stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(int value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of a 4-byte signed integer to the text stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(long value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of an 8-byte signed integer to the text stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(object value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of an object to the text stream by calling ToString on that object.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(float value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of a 4-byte floating-point value to the text stream.
Write Overloaded:
Write(string value)

Overridden:
Writes a string to the stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(uint value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of a 4-byte unsigned integer to the text stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(ulong value)

See base class member description: System.IO.TextWriter.Write


Writes the text representation of an 8-byte unsigned integer to the text stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(string format, object arg0)

See base class member description: System.IO.TextWriter.Write


Writes out a formatted string, using the same semantics as String.Format.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(string format, params object[] arg)

See base class member description: System.IO.TextWriter.Write


Writes out a formatted string, using the same semantics as String.Format.
Write Overloaded:
Write(char[] buffer, int index, int count)

Overridden:
Writes a subarray of characters to the stream.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(string format, object arg0, object arg1)

See base class member description: System.IO.TextWriter.Write


Writes out a formatted string, using the same semantics as String.Format.
Write
(inherited from System.IO.TextWriter)
Overloaded:
Write(string format, object arg0, object arg1, object arg2)

See base class member description: System.IO.TextWriter.Write


Writes out a formatted string, using the same semantics as String.Format.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine()

See base class member description: System.IO.TextWriter.WriteLine


Writes a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(bool value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of a Boolean followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(char value)

See base class member description: System.IO.TextWriter.WriteLine


Writes a character followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(char[] buffer)

See base class member description: System.IO.TextWriter.WriteLine


Writes an array of characters followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(decimal value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of a decimal value followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(double value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of a 8-byte floating-point value followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(int value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of a 4-byte signed integer followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(long value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of an 8-byte signed integer followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(object value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of an object by calling ToString on this object, followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(float value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of a 4-byte floating-point value followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(string value)

See base class member description: System.IO.TextWriter.WriteLine


Writes a string followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(uint value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of a 4-byte unsigned integer followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(ulong value)

See base class member description: System.IO.TextWriter.WriteLine


Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(string format, object arg0)

See base class member description: System.IO.TextWriter.WriteLine


Writes out a formatted string and a new line, using the same semantics as String.Format.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(string format, params object[] arg)

See base class member description: System.IO.TextWriter.WriteLine


Writes out a formatted string and a new line, using the same semantics as String.Format.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(char[] buffer, int index, int count)

See base class member description: System.IO.TextWriter.WriteLine


Writes a subarray of characters followed by a line terminator to the text stream.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(string format, object arg0, object arg1)

See base class member description: System.IO.TextWriter.WriteLine


Writes out a formatted string and a new line, using the same semantics as String.Format.
WriteLine
(inherited from System.IO.TextWriter)
Overloaded:
WriteLine(string format, object arg0, object arg1, object arg2)

See base class member description: System.IO.TextWriter.WriteLine


Writes out a formatted string and a new line, using the same semantics as String.Format.
Protected Fields
CoreNewLine
(inherited from System.IO.TextWriter)
See base class member description: System.IO.TextWriter.CoreNewLine


Stores the new line characters used for this TextWriter.
Protected Methods
Dispose Overridden:
Releases the unmanaged resources used by the StreamWriter and optionally releases the managed resources.
Finalize Overridden:
Frees the resources of the current StreamWriter before it is reclaimed by the garbage collector.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.IO.StreamWriter Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the StreamWriter class for the specified stream, using the default encoding and buffer size.
C# Syntax:
public StreamWriter(
   Stream stream
);
Parameters:

stream

The stream to write to.

Exceptions
Exception Type Condition
ArgumentException stream is not writable.
ArgumentNullException stream is null.
Remarks
This constructor creates a StreamWriter with UTF-8 encoding whose Encoding.GetPreamble method returns an empty byte array. The StreamWriter.BaseStream property is initialized using the stream parameter. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size.
C# Syntax:
public StreamWriter(
   string path
);
Parameters:

path

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

Exceptions
Exception Type Condition
UnauthorizedAccessException Access is denied.
ArgumentException path is an empty string("").
ArgumentNullException path is null.
DirectoryNotFoundException The directory to write to is not found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.
SecurityException The caller does not have the required permission.
Remarks
This constructor creates a StreamWriter with UTF-8 encoding whose Encoding.GetPreamble method returns an empty byte array.

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.

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    
.NET Framework Security:
FileIOPermission for reading and writing files. Associated enumeration: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding and the default buffer size.
C# Syntax:
public StreamWriter(
   Stream stream,
   Encoding encoding
);
Parameters:

stream

The stream to write to.

encoding

The character encoding to use.

Exceptions
Exception Type Condition
ArgumentNullException stream or encoding is null.
ArgumentException stream is not writable.
Remarks
This constructor initializes the StreamWriter.Encoding property using the encoding parameter, and the StreamWriter.BaseStream property using the stream parameter. For additional information, see TextWriter.Encoding. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    
See also:
Encoding

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the StreamWriter class for the specified file on the specified path, 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.
C# Syntax:
public StreamWriter(
   string path,
   bool append
);
Parameters:

path

The complete file path to write to.

append

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.

Exceptions
Exception Type Condition
UnauthorizedAccessException Access is denied.
ArgumentException path is empty.
ArgumentNullException path is null.
DirectoryNotFoundException The directory to write to is not found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.
SecurityException The caller does not have the required permission.
Remarks
This constructor creates a StreamWriter with UTF-8 encoding whose Encoding.GetPreamble method returns an empty byte array. For additional information, see TextWriter.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.

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    
.NET Framework Security:
FileIOPermission for reading and writing files. Associated enumeration: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded ctor #5
Summary
Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding and buffer size.
C# Syntax:
public StreamWriter(
   Stream stream,
   Encoding encoding,
   int bufferSize
);
Parameters:

stream

The stream to write to.

encoding

The character encoding to use.

bufferSize

Sets the buffer size.

Exceptions
Exception Type Condition
ArgumentNullException stream or encoding is null.
ArgumentOutOfRangeException bufferSize is negative.
ArgumentException stream is not writable.
Remarks
This constructor initializes the StreamWriter.Encoding property using the encoding parameter, and the StreamWriter.BaseStream property using the stream parameter. For additional information, see TextWriter.Encoding. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    
See also:
Encoding

Return to top


Overloaded ctor #6
Summary
Initializes a new instance of the StreamWriter class for the specified file on the specified path, 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.
C# Syntax:
public StreamWriter(
   string path,
   bool append,
   Encoding encoding
);
Parameters:

path

The complete file path to write to.

append

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.

encoding

The character encoding to use.

Exceptions
Exception Type Condition
UnauthorizedAccessException Access is denied.
ArgumentException path is empty.
ArgumentNullException path is null.
DirectoryNotFoundException The directory to write to is not found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.
SecurityException The caller does not have the required permission.
Remarks
This constructor initializes the StreamWriter.Encoding property using the encoding parameter. For additional information, see TextWriter.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.

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    
.NET Framework Security:
FileIOPermission for reading and writing files. Associated enumeration: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write
See also:
Encoding

Return to top


Overloaded ctor #7
Summary
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.
C# Syntax:
public StreamWriter(
   string path,
   bool append,
   Encoding encoding,
   int bufferSize
);
Parameters:

path

The complete file path to write to.

append

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.

encoding

The character encoding to use.

bufferSize

Sets the buffer size.

Exceptions
Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path or encoding is null.
ArgumentOutOfRangeException bufferSize is negative.
FileNotFoundException The file cannot be found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label syntax.
SecurityException The caller does not have the required permission.
UnauthorizedAccessException Access is denied.
PathTooLongException The specified path, file name, or both are too long. After full qualification, each must be less than 256 characters.
Remarks
This constructor initializes the StreamWriter.Encoding property using the encoding parameter. For additional information, see TextWriter.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.

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		public void CreateTextFile(string fileName, string textToAdd) {
			string logFile = DateTime.Now.ToShortDateString()
				.Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
			
			FileStream fs = new FileStream(fileName,
				FileMode.CreateNew, FileAccess.Write, FileShare.None);
			
			StreamWriter swFromFile = new StreamWriter(logFile);
			swFromFile.Write(textToAdd);
			swFromFile.Flush();
			swFromFile.Close();

			StreamWriter swFromFileStream = new StreamWriter(fs);
			swFromFileStream.Write(textToAdd);
			swFromFileStream.Flush();
			swFromFileStream.Close();

			StreamWriter swFromFileStreamDefaultEnc = 
				new System.IO.StreamWriter(fs, 
				System.Text.Encoding.Default);
			swFromFileStreamDefaultEnc.Write(textToAdd);
			swFromFileStreamDefaultEnc.Flush();
			swFromFileStreamDefaultEnc.Close();

			StreamWriter swFromFileTrue = 
				new StreamWriter(fileName,true);
			swFromFileTrue.Write(textToAdd);
			swFromFileTrue.Flush();
			swFromFileTrue.Close();
			
			StreamWriter swFromFileTrueUTF8Buffer = 
				new StreamWriter(fileName, 
				true, System.Text.Encoding.UTF8,512);
			swFromFileTrueUTF8Buffer.Write(textToAdd);
			swFromFileTrueUTF8Buffer.Flush();
			swFromFileTrueUTF8Buffer.Close();

			StreamWriter swFromFileTrueUTF8 = 
				new StreamWriter(fileName, true,
				System.Text.Encoding.UTF8);
			swFromFileTrueUTF8.Write(textToAdd);
			swFromFileTrueUTF8.Flush();
			swFromFileTrueUTF8.Close();

			StreamWriter swFromFileStreamUTF8Buffer = 
				new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
			swFromFileStreamUTF8Buffer.Write(textToAdd);
			swFromFileStreamUTF8Buffer.Flush();
			swFromFileStreamUTF8Buffer.Close();
		}

    
.NET Framework Security:
FileIOPermission for reading and writing files. Associated enumeration: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write
See also:
Encoding

Return to top


Field: CoreNewLine
Inherited
See base class member description: System.IO.TextWriter.CoreNewLine

Summary
Stores the new line characters used for this TextWriter.
C# Syntax:
protected char[] CoreNewLine;

Return to top


Field: Null
Summary
Provides a StreamWriter with no backing store that can be written to, but not read from.
C# Syntax:
public static readonly StreamWriter Null;
Remarks
Use Null to redirect output to a StreamWriter that will not consume any operating system resources.

When the StreamWriter.Write methods are invoked on Null, the call simply returns, and no data is actually written to any backing store.

Example
			if(sw.Equals(StreamWriter.Null)) {
				sw.WriteLine("The store may be written to, but not read from.");
			}

    

Return to top


Property: AutoFlush (read-write)
Summary
Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to Console.Write or Console.WriteLine.
C# Syntax:
public virtual bool AutoFlush {get; set;}
Remarks
Flushing the stream will not flush its underlying encoder unless you explicitly call StreamWriter.Flush or StreamWriter.Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

When AutoFlush is set to false, StreamWriter will do a limited amount of buffering, both internally and potentially in the encoder from the encoding you passed in. You may get better performance by setting AutoFlush to false, assuming that you always call Close (or at least Flush) when you're done writing with a StreamWriter.

For example, set AutoFlush to true when you are writing to a device where the user expects immediate feedback.Console.Out is one of these cases: The StreamWriter used internally for writing to Console flushes all its internal state except the encoder state after every call to Write or WriteLine.

Example
			// Gets or sets a value indicating whether the StreamWriter
			// will flush its buffer to the underlying stream after every 
			// call to Console.Write or Console.WriteLine.
			sw.AutoFlush = true;							 

    

Return to top


Property: BaseStream (read-only)
Summary
Gets the underlying stream that interfaces with a backing store.
C# Syntax:
public virtual Stream BaseStream {get;}
Example
This example shows a use of BaseStream with Stream.Seek and SeekOrigin to set the file pointer of the underlying stream to the end.
 FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate,
    FileAccess.Write);
 // Create a Char writer.
 StreamWriter w = new StreamWriter(fs);
 // Set the StreamWriter file pointer to the end.
 w.BaseStream.Seek(0, SeekOrigin.End);

    

Return to top


Overridden Property: Encoding (read-only)
Summary
Gets the Encoding in which the output is written.
C# Syntax:
public override Encoding Encoding {get;}
Remarks
This is necessary for some XML scenarios where a header must be written containing the encoding used by the StreamWriter. This allows the XML code to consume an arbitrary StreamWriter and generate the correct XML header.
Example
			// Write out the current text encoding
			sw.WriteLine("Encoding: {0}",
				sw.Encoding.ToString());

    

Return to top


Property: FormatProvider (read-only)
Inherited
See base class member description: System.IO.TextWriter.FormatProvider

Summary
Gets an object that controls formatting.
C# Syntax:
public virtual IFormatProvider FormatProvider {get;}

Return to top


Property: NewLine (read-write)
Inherited
See base class member description: System.IO.TextWriter.NewLine

Summary
Gets or sets the line terminator string used by the current TextWriter.
C# Syntax:
public virtual string NewLine {get; set;}
Remarks
The default line terminator string is a carriage return followed by a line feed ("\r\n").

The line terminator string is written to the text stream whenever one of the WriteLine methods is called. In order for text written by the TextWriter to be readable by a TextReader, only "\n" or "\r\n" should be used as terminator strings. If NewLine is set to null, the default newline character is used instead.

Return to top


Overridden Method: Close()
Summary
Closes the current StreamWriter and the underlying stream.
C# Syntax:
public override void Close();
Remarks
This method overrides Stream.Close.

This implementation of Close calls the StreamWriter.Dispose method passing a true value.

You must call Close to ensure that all data is correctly written out to the underlying stream. Following a call to Close, any operations on the StreamWriter might raise exceptions.

Flushing the stream will not flush its underlying encoder unless you explicitly call StreamWriter.Flush or Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Example
            // close the file by closing the writer
			sw.Close();

    

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Overridden Method: Dispose(
   bool disposing
)
Summary
Releases the unmanaged resources used by the StreamWriter and optionally releases the managed resources.
C# Syntax:
protected override void Dispose(
   bool disposing
);
Parameters:

disposing

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks
When the disposing parameter is true, this method releases all resources held by any managed objects that this StreamWriter references. This method invokes the Dispose() method of each referenced object.

Notes to inheritors: Dispose may be called multiple times by other objects. When overriding Dispose, be careful not to reference objects that have been previously disposed in an earlier call to Dispose.

This method calls the dispose method of the base class, TextWriter.Dispose.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: Finalize()
Summary
Frees the resources of the current StreamWriter before it is reclaimed by the garbage collector.
C# Syntax:
~StreamWriter();

Return to top


Overridden Method: Flush()
Summary
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
C# Syntax:
public override void Flush();
Exceptions
Exception Type Condition
ObjectDisposedException The current writer is closed.
IOException An I/O error has occurred.
Remarks
This method overrides TextWriter.Flush.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or StreamWriter.Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

See also:
Stream.Close

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 public class MyClass : MarshalByRefObject
 {
   public override Object InitializeLifetimeService()
   {
     ILease lease = (ILease)base.InitializeLifetimeService();
     if (lease.CurrentState == LeaseState.Initial)
     {
          lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
          lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
           lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
     }
       return lease;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Write(
   bool value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of a Boolean value to the text stream.
C# Syntax:
public virtual void Write(
   bool value
);
Parameters:

value

The Boolean to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Boolean.ToString.

This method outputs either Boolean.TrueString or Boolean.FalseString.

Return to top


Overloaded Method: Write(
   char value
)
Summary
Writes a character to the stream.
C# Syntax:
public override void Write(
   char value
);
Parameters:

value

The character to write to the text stream.

Exceptions
Exception Type Condition
IOException An I/O error occurs.
ObjectDisposedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.
NotSupportedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.
Remarks
This method overrides TextWriter.Write.

The specified character is written to the underlying stream unless the end of the stream is reached prematurely. If StreamWriter.AutoFlush is true, StreamWriter.Flush is invoked automatically.

Return to top


Overloaded Method: Write(
   char[] buffer
)
Summary
Writes a character array to the stream.
C# Syntax:
public override void Write(
   char[] buffer
);
Parameters:

buffer

A character array containing the data to write. If buffer is null, nothing is written.

Exceptions
Exception Type Condition
IOException An I/O error occurs.
ObjectDisposedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.
NotSupportedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.
Remarks
This method overrides System.IO.TextWriter.Write.

The specified characters are written to the underlying stream unless the end of the stream is reached prematurely. If StreamWriter.AutoFlush is true, StreamWriter.Flush is invoked automatically.

This method might provide faster performance than Write (char[], int, int) because it has fewer arguments to check.

Return to top


Overloaded Method: Write(
   decimal value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of a decimal value followed by a line terminator to the text stream.
C# Syntax:
public virtual void Write(
   decimal value
);
Parameters:

value

The decimal value to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs.

Return to top


Overloaded Method: Write(
   double value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of an 8-byte floating-point value to the text stream.
C# Syntax:
public virtual void Write(
   double value
);
Parameters:

value

The 8-byte floating-point value to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Double.ToString.

Return to top


Overloaded Method: Write(
   int value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of a 4-byte signed integer to the text stream.
C# Syntax:
public virtual void Write(
   int value
);
Parameters:

value

The 4-byte signed integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Int32.ToString.

Return to top


Overloaded Method: Write(
   long value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of an 8-byte signed integer to the text stream.
C# Syntax:
public virtual void Write(
   long value
);
Parameters:

value

The 8-byte signed integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Int64.ToString.

Return to top


Overloaded Method: Write(
   object value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of an object to the text stream by calling ToString on that object.
C# Syntax:
public virtual void Write(
   object value
);
Parameters:

value

The object to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
This version of TextWriter.Write is equivalent to (value.ToString ()).

If the specified object is null, no action is taken and no exception is thrown. Otherwise, the object's ToString method is called to produce the string representation, and the resulting string is then written to the output stream.

Return to top


Overloaded Method: Write(
   float value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of a 4-byte floating-point value to the text stream.
C# Syntax:
public virtual void Write(
   float value
);
Parameters:

value

The 4-byte floating-point value to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Single.ToString.

Return to top


Overloaded Method: Write(
   string value
)
Summary
Writes a string to the stream.
C# Syntax:
public override void Write(
   string value
);
Parameters:

value

The string to write to the stream. If value is null, nothing is written.

Exceptions
Exception Type Condition
ObjectDisposedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.
NotSupportedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.
IOException An I/O error occurs.
Remarks
This method overrides TextWriter.Write.

The specified String is written to the underlying stream unless the end of the stream is reached prematurely.

StreamWriter.Flush is invoked automatically if StreamWriter.AutoFlush is true. If value is null, no entries are written.

Return to top


Overloaded Method: Write(
   uint value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of a 4-byte unsigned integer to the text stream.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public virtual void Write(
   uint value
);
Parameters:

value

The 4-byte unsigned integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling UInt32.ToString.

Return to top


Overloaded Method: Write(
   ulong value
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes the text representation of an 8-byte unsigned integer to the text stream.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public virtual void Write(
   ulong value
);
Parameters:

value

The 8-byte unsigned integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling UInt64.ToString.

Return to top


Overloaded Method: Write(
   string format,
   object arg0
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes out a formatted string, using the same semantics as String.Format.
C# Syntax:
public virtual void Write(
   string format,
   object arg0
);
Parameters:

format

The formatting string.

arg0

An object to write into the formatted string.

Exceptions
Exception Type Condition
ArgumentNullException format or arg0 is null.

-or-

An object referenced in the format string is null.

IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to the number of provided objects to be formatted .

Remarks
This version of TextWriter.Write is equivalent to ( (format, arg0, arg1)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: Write(
   string format,
   params object[] arg
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes out a formatted string, using the same semantics as String.Format.
C# Syntax:
public virtual void Write(
   string format,
   params object[] arg
);
Parameters:

format

The formatting string.

arg

The object array to write into the formatted string.

Exceptions
Exception Type Condition
ArgumentNullException format or arg is null.
IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to arg.Length.

Remarks
This version of TextWriter.Write is equivalent to ( (format, arg)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: Write(
   char[] buffer,
   int index,
   int count
)
Summary
Writes a subarray of characters to the stream.
C# Syntax:
public override void Write(
   char[] buffer,
   int index,
   int count
);
Parameters:

buffer

A character array containing the data to write.

index

The index into buffer at which to begin writing.

count

The number of characters to read from buffer.

Exceptions
Exception Type Condition
ArgumentNullException buffer is null.
ArgumentException The buffer length minus index is less than count.
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs.
ObjectDisposedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.
NotSupportedException StreamWriter.AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.
Remarks
This method overrides TextWriter.Write.

The characters are read from buffer beginning at index and continuing through index + (count - 1). All characters are written to the underlying stream unless the end of the underlying stream is reached prematurely. StreamWriter.Flush is invoked automatically if StreamWriter.AutoFlush is true.

Example
This example writes eight characters from a 13-element array to a file, beginning at the third element of the array.
 using System;
 using System.IO;
 
 public class SWBuff 
 {
 public static void Main(String[] args)
   {
 FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
 char[] b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
 StreamWriter sw = new StreamWriter(sb);
 sw.Write(b, 3, 8);
 sw.Close();
   }
 }
 
 //This code writes "defghijk" to MyFile.txt.

    

Return to top


Overloaded Method: Write(
   string format,
   object arg0,
   object arg1
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes out a formatted string, using the same semantics as String.Format.
C# Syntax:
public virtual void Write(
   string format,
   object arg0,
   object arg1
);
Parameters:

format

The formatting string.

arg0

An object to write into the formatted string.

arg1

An object to write into the formatted string.

Exceptions
Exception Type Condition
ArgumentNullException format or any of the arg parameters is null.

-or-

An object referenced in the format string is null.

IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to the number of provided objects to be formatted .

Remarks
This version of TextWriter.Write is equivalent to ( (format, arg0, arg1)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: Write(
   string format,
   object arg0,
   object arg1,
   object arg2
)
Inherited
See base class member description: System.IO.TextWriter.Write

Summary
Writes out a formatted string, using the same semantics as String.Format.
C# Syntax:
public virtual void Write(
   string format,
   object arg0,
   object arg1,
   object arg2
);
Parameters:

format

The formatting string.

arg0

An object to write into the formatted string.

arg1

An object to write into the formatted string.

arg2

An object to write into the formatted string.

Exceptions
Exception Type Condition
ArgumentNullException format or any of the arg parameters is null.

-or-

An object referenced in the format string is null.

IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to the number of provided objects to be formatted .

Remarks
This version of TextWriter.Write is equivalent to ( (format, arg0, arg1, arg2)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: WriteLine()
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine();
Return Value:
The default line terminator is a carriage return followed by a line feed ("\r\n"), but this value can be changed using the TextWriter.NewLine property.
Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.

Return to top


Overloaded Method: WriteLine(
   bool value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of a Boolean followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   bool value
);
Parameters:

value

The Boolean to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Boolean.ToString.

This method outputs either Boolean.TrueString or Boolean.FalseString.

Return to top


Overloaded Method: WriteLine(
   char value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes a character followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   char value
);
Parameters:

value

The character to write to the text stream.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
This version of TextWriter.WriteLine is equivalent to (buffer) followed by ().

Return to top


Overloaded Method: WriteLine(
   char[] buffer
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes an array of characters followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   char[] buffer
);
Parameters:

buffer

The character array from which data is read.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
All the characters in buffer are written to the underlying stream. If the character array is null, nothing is written.

This version of TextWriter.WriteLine is equivalent to (buffer) followed by ().

Return to top


Overloaded Method: WriteLine(
   decimal value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of a decimal value followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   decimal value
);
Parameters:

value

The decimal value to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs.

Return to top


Overloaded Method: WriteLine(
   double value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of a 8-byte floating-point value followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   double value
);
Parameters:

value

The 8-byte floating-point value to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.

Return to top


Overloaded Method: WriteLine(
   int value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of a 4-byte signed integer followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   int value
);
Parameters:

value

The 4-byte signed integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Int32.ToString.

Return to top


Overloaded Method: WriteLine(
   long value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of an 8-byte signed integer followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   long value
);
Parameters:

value

The 8-byte signed integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling Int64.ToString.

Return to top


Overloaded Method: WriteLine(
   object value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of an object by calling ToString on this object, followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   object value
);
Parameters:

value

The object to write. If value is null, only the line termination characters are written.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
This version of TextWriter.WriteLine is equivalent to (value.ToString ()).

Return to top


Overloaded Method: WriteLine(
   float value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of a 4-byte floating-point value followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   float value
);
Parameters:

value

The 4-byte floating-point value to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.

Return to top


Overloaded Method: WriteLine(
   string value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes a string followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   string value
);
Parameters:

value

The string to write. If value is null, only the line termination characters are written.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
This version of TextWriter.WriteLine is equivalent to (value.ToByteArray ()).

The line terminator string is defined by the TextWriter.NewLine property.

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

Return to top


Overloaded Method: WriteLine(
   uint value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of a 4-byte unsigned integer followed by a line terminator to the text stream.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public virtual void WriteLine(
   uint value
);
Parameters:

value

The 4-byte unsigned integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling UInt32.ToString.

Return to top


Overloaded Method: WriteLine(
   ulong value
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text stream.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public virtual void WriteLine(
   ulong value
);
Parameters:

value

The 8-byte unsigned integer to write.

Exceptions
Exception Type Condition
IOException An I/O error occurs, such as the stream being closed.
Remarks
The text representation of the specified value is produced by calling UInt64.ToString.

Return to top


Overloaded Method: WriteLine(
   string format,
   object arg0
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes out a formatted string and a new line, using the same semantics as String.Format.
C# Syntax:
public virtual void WriteLine(
   string format,
   object arg0
);
Parameters:

format

The formatted string.

arg0

The object to write into the formatted string.

Exceptions
Exception Type Condition
ArgumentNullException A string or object is passed in as null.

-or-

An object referenced in the format string is null.

IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to the number of provided objects to be formatted .

Remarks
This version of TextWriter.WriteLine is equivalent to ( (format, arg0, arg1)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: WriteLine(
   string format,
   params object[] arg
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes out a formatted string and a new line, using the same semantics as String.Format.
C# Syntax:
public virtual void WriteLine(
   string format,
   params object[] arg
);
Parameters:

format

The formatting string.

arg

The object array to write into format string.

Exceptions
Exception Type Condition
ArgumentNullException A string or object is passed in as null.
IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to arg.Length.

Remarks
This version of TextWriter.WriteLine is equivalent to ( (format, arg)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: WriteLine(
   char[] buffer,
   int index,
   int count
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes a subarray of characters followed by a line terminator to the text stream.
C# Syntax:
public virtual void WriteLine(
   char[] buffer,
   int index,
   int count
);
Parameters:

buffer

The character array from which data is read.

index

The index into buffer at which to begin reading.

count

The maximum number of characters to write.

Return Value:
Characters are read from buffer beginning at index and ending at index + count.
Exceptions
Exception Type Condition
ArgumentException The buffer length minus index is less than count.
ArgumentNullException The buffer parameter is Null.
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs, such as the stream being closed.
Remarks
This method will write count characters of data into this TextWriter from the buffer character array starting at position index.

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

This version of TextWriter.WriteLine is equivalent to (buffer[i]) for each character in buffer between index and (index + count) followed by ().

Return to top


Overloaded Method: WriteLine(
   string format,
   object arg0,
   object arg1
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes out a formatted string and a new line, using the same semantics as String.Format.
C# Syntax:
public virtual void WriteLine(
   string format,
   object arg0,
   object arg1
);
Parameters:

format

The formatting string.

arg0

The object to write into format string.

arg1

The object to write into the formatted string.

Exceptions
Exception Type Condition
ArgumentNullException A string or object is passed in as null.

-or-

An object referenced in the format string is null.

IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to the number of provided objects to be formatted.

Remarks
This version of TextWriter.WriteLine is equivalent to ( (format, arg0, arg1)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Overloaded Method: WriteLine(
   string format,
   object arg0,
   object arg1,
   object arg2
)
Inherited
See base class member description: System.IO.TextWriter.WriteLine

Summary
Writes out a formatted string and a new line, using the same semantics as String.Format.
C# Syntax:
public virtual void WriteLine(
   string format,
   object arg0,
   object arg1,
   object arg2
);
Parameters:

format

The formatting string.

arg0

The object to write into format string.

arg1

The object to write into format string.

arg2

The object to write into format string.

Exceptions
Exception Type Condition
ArgumentNullException A string or object is passed in as null.

-or-

An object referenced in the format string is null.

IOException An I/O error occurs, such as the stream being closed.
FormatException The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or larger than or equal to the number of provided objects to be formatted.

Remarks
This version of TextWriter.WriteLine is equivalent to ( (format, arg0, arg1, arg2)).

This method does not search the specified String for individual newline characters(hexadecimal 0x000a) and replace them with TextWriter.NewLine.

If a specified object is not referenced in the format string, it is ignored.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.