C# StreamReader CurrentEncoding
Description
StreamReader CurrentEncoding
Gets the current character
encoding that the current StreamReader object is using.
Syntax
StreamReader.CurrentEncoding
has the following syntax.
public virtual Encoding CurrentEncoding { get; }
Example
The following code example gets the encoding of the specified StreamReader object.
/*from w w w .ja v a 2 s . co m*/
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
using (StreamWriter sw = new StreamWriter(path, false, new UnicodeEncoding()))
{
sw.WriteLine("This");
sw.WriteLine("is from java2s.com");
}
using (StreamReader sr = new StreamReader(path, true))
{
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read());
}
Console.WriteLine("The encoding used was {0}.", sr.CurrentEncoding);
}
}
}