C#使用UTF8写入文件

时间:2017-04-26 19:27:15

标签: c# selenium selenium-webdriver utf-8

我有这个简单的代码:

using (var sw = new StreamWriter(path + kat + "/" + namec, true, Encoding.UTF8))
{
    sw.WriteLine(namex + "," + address + "," + web + "," + email + "," + phone + "," + linksx + "," + transport);
}

我使用Selenium Web Driver从丹麦网站下载数据,示例链接:http://www.visitdenmark.dk/da/danmark/danhostel-roenne-gdk614558

然后我将它存储在.csv文件中,但我仍然有这样的字符Ă而不是这个ø。正如你所看到我设置Encoding.UTF8当我将bool设置为false时,它变得更有趣,然后一切正常,但它对我没有帮助,因为我需要用新数据附加该文件。我怎样才能解决这个问题 ?

1 个答案:

答案 0 :(得分:1)

你应该使用windows-1252(或Encoding.GetEncoding(1252))而不是UTF-8,用于丹麦语。

编辑:

缺少BOM问题导致Excel无法正确读取文件。

// notice we are creating a new file here
using (var writer = new StreamWriter(path, false, Encoding.UTF8))
{
    // Add a BOM in the beginning of the file
    var preamble = Encoding.UTF8.GetPreamble();
    writer.BaseStream.Write(preamble, 0, preamble.Length);

    // write data...
    writer.WriteLine(string.Join(",", "Danhostel Rønne", "Arsenalvej 12 3700 Rønne"));
}

// when you write the same file again, you don't need to append the BOM again
using (var writer = new StreamWriter(path, true, Encoding.UTF8))
{
    // write more data...
    writer.WriteLine(string.Join(",", "Danhostel Rønne", "Arsenalvej 12 3700 Rønne"));
}