0

I'm writing a service that will read a file from a directory, read the contents of the file and process the content.

I am having issues with Swedish characters that are read from the file as they are beeing translated into "garbage chars" by the service when they are read.

Does anyone know what default code page/culture setting is beeing used by the Service Control Manager or perhaps you know of any article about "best practice" for handling Swedish characters in the context of Windows Service programming?

Any help is appreciated.

  • How do you read the file? I'd bet this is where the characters are garbled up, not by the fact that your program runs as a service. Can you show us your code? – dtb Jun 14 '10 at 13:10
  • You were correct. I didn't take into account the Encoding option in the StreamReader constructor and that was my problem. I've modified my code and now it works. Thank you! –  Jun 15 '10 at 08:00

1 Answers1

0

It has probably some thing to do with how you read the file and how the file is encoded. For example if the file is encoded with UTF-8 and your reading it with ASCII then the special characters will be garbage.

For example:

using (var fileStream = new StreamReader(@"path to file", Encoding.UTF8))
{
    Console.Write(fileStream.ReadToEnd());
}

You can change Encoding.UTF8 to match the encoding used to create the file.

Jens Granlund
  • 4,950
  • 1
  • 31
  • 31
  • Thank you for your pointer. I just had to figure out which encoding to use and that made it work. I used Encoding.GetEncoding(1252) in the StreamReader construtcor and that made my day. Thank you! –  Jun 15 '10 at 07:58