This documentation is archived and is not being maintained.

StreamWriter.Encoding Property

Gets the Encoding in which the output is written.

[Visual Basic]
Overrides Public ReadOnly Property Encoding As Encoding
[C#]
public override Encoding Encoding {get;}
[C++]
public: __property Encoding* get_Encoding();
[JScript]
public override function get Encoding() : Encoding;

Property Value

The Encoding specified in the constructor for the current instance, or UTF8Encoding if an encoding was not specified.

Remarks

This property 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.

For an example of using this property, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File

Example

[Visual Basic, C#, C++] The following example retrieves the encoding of the specified StreamWriter instance.

[Visual Basic] 
' Write out the current text encoding.
Sw.WriteLine("Encoding: {0}", _
 Sw.Encoding.ToString())

[C#] 
// Write out the current text encoding
sw.WriteLine("Encoding: {0}",
    sw.Encoding.ToString());

[C++] 
// Write out the current text encoding
sw->WriteLine(S"Encoding: {0}",
   sw->Encoding);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

StreamWriter Class | StreamWriter Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show: