C#中将文件保存为utf-8无bom格式 UTF8Encoding(false)

分类:.Net知识问答| 发布:佚名| 查看: | 发表时间:2014/6/12

在讲魅族M9的通讯录导出为xml格式之后, 又转换成vCard(.vcf),导出的名片后,在红米手机里无法导入,提示格式不对,最后发现,原来红米手机不支持utf8+BOM编码的名片导入。

EditPlus 3 另存为编码选择
因此,修改了c#转换导出的编码:

/// <summary> 
/// 保存生成html页面到相应路径
/// </summary>
/// <param name="Shtml">要写入的内容</param>
/// <param name="TemplatePath">存储路径</param>
/// <returns></returns>
public static void WriteHtmlFile(string Shtml, string HtmlPath)
{
StreamWriter sw = null;
try
{
UTF8Encoding utf8 = new UTF8Encoding(false);
using (sw = new StreamWriter(HtmlPath, false, utf8))
{
sw.Write(Shtml);
}
}
catch
{
throw new Exception("存储路径错误,请检查路径" + HtmlPath + "是否存在!");
}
finally
{
sw.Close();
}
}

关于UTF8 与 UTF8 +BOM 区别

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/1205.html