我正在嘗試使用帶有UTF8編碼的VB.Net創建文本文件,而沒有BOM。誰能幫我,怎么做?我可以使用UTF8編碼寫入文件,但是如何從其中刪除字節順序標記?edit1:我嘗試過這樣的代碼; Dim utf8 As New UTF8Encoding() Dim utf8EmitBOM As New UTF8Encoding(True) Dim strW As New StreamWriter("c:\temp\bom\1.html", True, utf8EmitBOM) strW.Write(utf8EmitBOM.GetPreamble()) strW.WriteLine("hi there") strW.Close() Dim strw2 As New StreamWriter("c:\temp\bom\2.html", True, utf8) strw2.Write(utf8.GetPreamble()) strw2.WriteLine("hi there") strw2.Close()1.html僅使用UTF8編碼創建,而2.html使用ANSI編碼格式創建。簡化方法-http: //whatilearnttuday.blogspot.com/2011/10/write-text-files-without-byte-order.html
3 回答

當年話下
TA貢獻1890條經驗 獲得超9個贊
嘗試這個:
Encoding outputEnc = new UTF8Encoding(false); // create encoding with no BOM
TextWriter file = new StreamWriter(filePath, false, outputEnc); // open file with encoding
// write data here
file.Close(); // save and close it

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
只需簡單地使用的方法WriteAllText從System.IO.File。
請檢查File.WriteAllText中的示例。
此方法使用不帶字節序標記(BOM)的UTF-8編碼,因此使用GetPreamble方法將返回一個空字節數組。如果必須在文件的開頭包含UTF-8標識符(例如字節順序標記),請使用帶有UTF8編碼的WriteAllText(String,String,Encoding)方法重載。
- 3 回答
- 0 關注
- 549 瀏覽
添加回答
舉報
0/150
提交
取消