c#如何生成不带签名的utf-8文件

来源:百度知道 编辑:UC知道 时间:2024/06/05 05:13:56
最近需要生成shtml的静态页面。include的文件要求不带签名。于是写了以下方法

public static void WriteFile(string Url,string content)
{
byte[] contents = Encoding.UTF8.GetBytes(content.ToCharArray(),0,content.Length );
using (FileStream outstream = System.IO.File.Create(Url, contents.Length))
{
outstream.Write(contents, 0, contents.Length);
outstream.Close();
}
}

此方法保存的文件发现是竟然是gb2312的

FileWriter = new StreamWriter(tempFilePath, false, Code);
FileWriter.Write(strFile);
FileWriter.Flush();
写进去变成utf-8了 但是却仍然带签名
byte[] contents = Encoding.UTF8.GetBytes(content.ToCharArray(),0,content.Length );
此句中,可以把ToCharArray去掉,因该函数也进行一次编码转换。
改成byte[] contents = Encoding.UTF8.GetBytes(content);试试。

修改后和第二个方案一样 变成带签名的utf-8
始终没办法去掉那个签名。
本来查资料说 签名就是文件的头三个byte 结果我尝试从第四个字节开始写入后发现字符串前三个被却

使用 Encoding.UTF8 是自动带 三 byte 的 BOM,如果要不添加 BOM。应该改用 UTF8Encoding utf8 = new UTF8Encoding(是否添加 BOM);

byte[] contents = Encoding.UTF8.GetBytes(content.ToCharArray(),0,content.Length );
此句中,可以把ToCharArray去掉,因该函数也进行一次编码转换。
改成byte[] contents = Encoding.UTF8.GetBytes(content);试试。