Home > Enterprise >  C # - .Net Core Write an ANSI encoded text file
C # - .Net Core Write an ANSI encoded text file

Time:10-21

I have to create an ANSI encoded txt file, because the system where I have to load it only reads ANSI and does not read UTF-8. I tried to follow various threads but nothing worked, now my code is this:

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding(1252));
            sw.Write(sb.ToString());
            sw.Close();
            sb.Clear();

When I open the generated file in Notepad I see that it is in UTF-8. Can someone help me? Thanks

CodePudding user response:

Notepad guesses the encoding based on the file's contents, as text files don't include their used encoding (unless they start with a BOM).

If you write using code page 1252, then that's what's used.

  • Related