Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Danish characters from text files

Tags:

c#

I am trying to read text files that contains some danish characters. I have found a few ways to do it using different types of encoding but the examples I have seen are reading just one file. This is what I have so far.

//search directory for all .txt files
foreach (string files in Directory.GetFiles(@"C:\ftp\inbox", "*.txt"))
{     
    //The 'using' command close connection when it is done
    using (var reader = new StreamReader(File.OpenRead(files)))               
    {
        // Handle contents
    }
}

My problem is all the characters are being read as �. I need them to be read in as they are.

like image 877
robert woods Avatar asked Dec 08 '22 17:12

robert woods


1 Answers

I resolved the issue by changing:

using (var reader = new StreamReader(File.OpenRead(files)))

into

using (var reader = new StreamReader(files, System.Text.Encoding.GetEncoding("iso-8859-1"))) 

and now the danish characters are read in as they should be.

like image 104
robert woods Avatar answered Dec 11 '22 09:12

robert woods