Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead Unicode text from a file in Visual Basic .NET
DescriptionThis example shows how to read Unicode text from a file in Visual Basic .NET.
KeywordsUnicode, text, file, StreamReader, OpenText, VB.NET
CategoriesVB.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
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated