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



The information in this article 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 Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • 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("c:\tmp\TestFile.txt", _
               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("c:\\tmp\\TestFile.txt", 
			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:For more information about the Encoding class, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:3/6/2006
Keywords:kbvs2005swept kbvs2005applies kbFileIO kbProgramming kbinfo kbtshoot kbprb KB889835 kbAudDeveloper