• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Foraing characters missing when I read a Text file

Here it is a simply routine to load a file into a Hashtable, everything works fine, but when I use some characters in text file like ñ, á,é,í,ó,ú
these characters doesn't appear in the read strings.

I create the text file with Visual Studio.NET 2003 Pro.

Can someone help me?

LoadFromFile(Hashtable aTable, string aFileName){
                          FileInfo sesFile = new FileInfo(aFileName);
                  if (!sesFile.Exists) return false;
                  string [] data;
                  string lin;
                  StreamReader str = sesFile.OpenText();
                        while ((lin=str.ReadLine())!=null){
                              lin = lin.Trim();
                              if ((lin != "") && (lin.IndexOf(';') != 0)){
                                    data = lin.Split('=');
                                    aTable.Add(data[0].Trim(), data[1].Trim());
                  return true;

Text file:

In code you get:
String1=Len  (Missing ó)
String2=Nio  (Missing ñ)

1 Solution
Add an encoding parameter to the StreamReader's constructor:

StreamReader str = new StreamReader(sesFile.OpenText(), Encoding.GetEncoding("ISO-8859-1"));

I'm not sure exactly what encoding your file is using, so swap out "ISO-8859-1" for whatever it is.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now