locked
How to Create ANSI file using C# RRS feed

  • Question

  • Hello Everyone,

    I have  to create an ANSI file using C#.

    My real problem is I have a File in ANSI  enconding format comming from a Linux API(I do not have access to this machine, I just may use the API)

    and I need to create a copy of this file and change the content of this new file.

    The problem is, I do a copy of this file and it keeps the enconding howerver when I try to clear the content of this file to write the new content it changes automatically the encondig of this new file.

    These are my steps:

    1) Make Copy the ANSI FIle (ok)

    File.Copy("source.txt", "new_file.txt");

    2)Erase the content of this new file

    File.WriteAllText("new_file.txt", string.Empty, Encoding.Default);

    In this point it has changed the enconding of this new file

    Please, someone has an idea about this issue?

    Regards,

    William

    Wednesday, April 11, 2018 2:03 PM

All replies

  • The default encoding for .NET is Unicode. When saving the file use Encoding.Ascii instead of Encoding.Default if you are limiting yourself to ASCII characters. Otherwise use Utf8 instead and see if that gets back to Linux correctly.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 11, 2018 4:26 PM