|
|
Title | Read Unicode text from a file in Visual Basic .NET |
Description | This example shows how to read Unicode text from a file in Visual Basic .NET. |
Keywords | Unicode, text, file, StreamReader, OpenText, VB.NET |
Categories | VB.NET, Files and Directories |
|
|
If you open a normal text file containing Unicode characters, the characters are converted into plain ASCII. To read the Unicode properly, save the file as Unicde text rather than plain text.
When the program loads, it reads the file Swedish Text.txt, a text file containing Unicode characters, and displays the result. The text box shows the basic ASCII characters but not the extended Unicode characters.
The program then reads file Swedish Unicode.txt, which was saved as Unicde text. This time the text box shows the Unicode characters.
The files look the same in WordPad. Use the Save As command and select the Unicode Text Document file type to save the files so the program can read it properly.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim file_name As String = Application.StartupPath
file_name = file_name.Substring(0, _
file_name.LastIndexOf("\") + 1)
' Read the file as text.
Dim sr As StreamReader
sr = OpenText(file_name & "Swedish Text.txt")
txtAsText.Text = sr.ReadLine()
sr.Close()
' Read the file as Unicode.
sr = OpenText(file_name & "Swedish Unicode.txt")
txtAsUnicode.Text = sr.ReadLine()
sr.Close()
txtAsText.Select(0, 0)
End Sub
|
|
|
|
|
|