StreamReader.CurrentEncoding Property gets character encoding
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Try
Dim sw As StreamWriter = New StreamWriter(path, False, New UnicodeEncoding())
sw.WriteLine("This")
sw.WriteLine("is ")
sw.WriteLine("a")
sw.WriteLine("test")
sw.Close()
Dim sr As StreamReader = New StreamReader(path, True)
Do While sr.Peek() >= 0
Console.Write(Convert.ToChar(sr.Read()))
Loop
Console.WriteLine("The encoding used was {0}.", sr.CurrentEncoding)
sr.Close()
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Related examples in the same category