Search code examples
c#encodingstringbuilder

StringBuilder and accent


I have a StringBuilder and I want to write a text containing accent to an csv file.

Code:

StringBuilder strbr = new StringBuilder();
strbr.AppendLine("ù;é;à");
File.WriteAllText(filePath + ".csv", strbr.ToString());

But when I open my csv file, there is only: é

The file "test.csv' correctly contain ù;é;à, but when I open it with Excel I have:

Excel screenshot

Maybe I missed a header for Excel?


Solution

  • The file will be saved with UTF-8 encoding, but read with default one, say Win-1251. You can explicitly specify an encoding (UTF8 in this case):

    File.WriteAllText(filePath + ".csv", strbr.ToString(), Encoding.UTF8);
    

    Edit: I'm very sorry for my initial misleading explanation (thanks to Patrick Hofman who has pointed it out). The actual problem is the absence of BOM (Bite Order Mark): by default File.WriteAllText writes the text in the UTF8 format without BOM:

    https://referencesource.microsoft.com/#mscorlib/system/io/file.cs,8a8ede9e1ec4fece

    public static void WriteAllLines(String path, IEnumerable<String> contents)
    { 
        // ...
        InternalWriteAllLines(new StreamWriter(path, false, StreamWriter.UTF8NoBOM), contents);
    }
    

    then Excel reads the file, doesn't see any BOM and thus tries to read the file using the default encoding.