Microsoft KB Archive/889835

= The StreamReader.ReadLine method removes extended characters from the line that is being read in Visual Studio .NET or in Visual Studio 2005 =

Article ID: 889835

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





SYMPTOMS
When you use the StreamReader.ReadLine method to try to read a text file that contains extended characters, the extended characters are removed from the line that is being read.

Note An example of an extended character is a character with an accent, such as the é, ò, or à characters.



CAUSE
This problem occurs when the text file is not saved by using Unicode Translation Format (UTF) encoding. By default, the StreamReader class uses UTF encoding. Therefore, when the ReadLine method reads the text-file line, the extended characters are not correctly read.



WORKAROUND
To work around this problem, you must specify the correct UTF encoding by using the StreamReader class, or you must save the text file by using UTF encoding.

The following example shows how to read a text file by using UTF encoding. This example uses the encoding System.Text.Encoding.Default property that corresponds to the system's current ANSI code page.

Visual Basic .NET or Visual Basic 2005
Imports System Imports System.IO Module module1 Sub Main Dim sr As StreamReader = New StreamReader(&quot;c:\tmp\TestFile.txt&quot;, _              System.Text.Encoding.Default) Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do           line = sr.ReadLine Console.WriteLine(line) Loop Until line Is Nothing sr.Close

End Sub End Module

C#
using System; using System.IO;

class Class1 {   static void Main(string[] args) {       StreamReader sr = new StreamReader(&quot;c:\\tmp\\TestFile.txt&quot;,             System.Text.Encoding.Default); string line; // Read and display the lines from the file until the end // of the file is reached. while ((line = sr.ReadLine) != null) Console.WriteLine(line); sr.Close; } } Note In Visual Studio 2005, by default, Program.cs is created.

Note When you pass the System.Text.Encoding.Default property as the second parameter of the StreamReader method, the system's current ANSI code page is used to decode the file.



MORE INFORMATION
For more information about the StreamReader class, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/system.io.streamreader(vs.71).aspx

For more information about the Encoding class, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/system.text.encoding(vs.71).aspx

Additional query words: VS2002 VS2003

Keywords: kbvs2005swept kbvs2005applies kbfileio kbprogramming kbinfo kbtshoot kbprb KB889835

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.