[問題] 讀取文字檔 ANSI 轉 Unicode
我利用 StreamReader 想讀取一ANSI編碼檔案進listbox
但listbox卻將中文顯示為方框
而讀取unicode檔案是可以的
StreamReader file = new StreamReader(openFileDialog1.FileName);
string content = file.ReadToEnd();
不過在讀ANSI編碼時
偵測到的編碼卻是 UTF-8:
file.CurrentEncoding.EncodingName; //值為 "Unicode (UTF-8)"
所以我嘗試用下面方法 將content從utf8轉成unicode:
Encoding utf8 = Encoding.UTF8;
Encoding unicode = Encoding.Unicode;
byte[] contentByte = utf8.GetBytes(content); //string轉byte[]
contentByte = Encoding.Convert(utf8, unicode, contentByte);
content = Encoding.Unicode.GetString(contentByte); //byte[]轉string
轉出來還是跟原本一樣 中文字都方框
請問是哪個環節出錯了? 感謝~!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.204.168.133
推
07/16 23:21, , 1F
07/16 23:21, 1F
→
07/16 23:32, , 2F
07/16 23:32, 2F
→
07/16 23:42, , 3F
07/16 23:42, 3F
→
07/17 11:35, , 4F
07/17 11:35, 4F
→
07/17 11:37, , 5F
07/17 11:37, 5F
→
07/17 13:06, , 6F
07/17 13:06, 6F
→
07/31 15:05, , 7F
07/31 15:05, 7F
→
07/31 15:11, , 8F
07/31 15:11, 8F
→
07/31 15:13, , 9F
07/31 15:13, 9F