0

I have a C# WinForm which reads a .csv-File with StreamReader evrything works fine but if I want to write the content out then all ä, ö, ü become a ?

I didnt add any ASCII to the C# Project is that the reason?

Simon B
  • 179
  • 1
  • 1
  • 9
  • "become a ?", how do you verify this? What encoding are you specifying when writing the file? What tool/program/code are you using to look at the file afterwards? – Lasse V. Karlsen Nov 28 '16 at 12:21
  • 2
    The reason is that you *did* use ASCII, instead of Unicode. Either the file was saved as ASCII instead of UTF8, or you hard-coded an ASCII encoding when reading the file. .NET uses Unicode throughout, you have to force the use of ASCII. – Panagiotis Kanavos Nov 28 '16 at 12:22
  • try System.Text.Encoding.UTF8 – Fang Nov 28 '16 at 12:23
  • I dont know what you mean Lasse.. but I read all of this with StreamReader put it inside a array and then I make the output with a label – Simon B Nov 28 '16 at 12:23
  • The default encoding when reading/writing text files in .NET is UTF8. You'll have to either (a) change the file creation code to use UTF8 or (b) find the ASCII encoding used to generate the file and specify it as a parameter when reading the file. All text related methods accept a CultureInfo parameter – Panagiotis Kanavos Nov 28 '16 at 12:24
  • @SimonB your file is ASCII. StreamReader assumes UTF8 or the system locale's codepage. Obviously, the file was created with a different codepage. Eg, the file was created with a German codepage but your machine uses a US locale. What codepage did you use when *creating* the file? – Panagiotis Kanavos Nov 28 '16 at 12:25
  • @PanagiotisKanavos I edited the .csv-file with Notepad++ (I hope u ment this) – Simon B Nov 28 '16 at 12:27
  • @Fang you mean I should write: using System.Text.Encoding.UTF8 – Simon B Nov 28 '16 at 12:28
  • There is no text but encoded text. Without the writer and readers agreeing on a specific encoding, you have a failed communication. UTF-8 is appropriate in most cases. – Tom Blodget Nov 28 '16 at 20:34

0 Answers0