Search code examples
c#csvutf-8ansi

convert .csv(ANSI) into .csv(UTF-8)


I have a japanese environment in my PC. So I need a program to convert my .csv ANSI into .csv UTF-8. I tried using this code:

string st = File.ReadAllText("C:\\Users\\user\\Desktop\\train1.csv");            
System.IO.File.WriteAllText("C:\\Users\\user\\Desktop\\train2.csv",st.ToString(), Encoding.UTF8); 

The file train2.csv is successfully created, however, the text become unreadable due to conversion. What is the proper way to do this?


Solution

  • I assume the input file encoding isn't detected correctly so you should define it

    string st = File.ReadAllText("C:\\Users\\user\\Desktop\\train1.csv", Encoding.Default); //ANSI
    

    Documentation:

    This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.

    Use the ReadAllText(String, Encoding) method overload when reading files that might contain imported text, because unrecognized characters may not be read correctly.