中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#讀取中文文件亂碼的解方法

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
FileStream aFile = new FileStream(SingleFile, FileMode.Open);
StreamReader sr = new StreamReader(aFile, Encoding.GetEncoding("gb2312"), true);
string FileContent = sr.ReadToEnd();
aFile.Close();
ProcessData Pd = new ProcessData();
Pd.ProceData(FileContent);

StreamReader 使用3個參數(shù) 最后一個自動檢測utf-8,中文大部分是gb2312,如果不是utf-8,就用gb2312


系統(tǒng)自帶utf 檢測 ,見如下

        private void DetectEncoding()
        {
            if (this.byteLen >= 2)
            {
                this._detectEncoding = false;
                bool flag = false;
                if ((this.byteBuffer[0] == 0xfe) && (this.byteBuffer[1] == 0xff))
                {
                    this.encoding = new UnicodeEncoding(true, true);
                    this.CompressBuffer(2);
                    flag = true;
                }
                else if ((this.byteBuffer[0] == 0xff) && (this.byteBuffer[1] == 0xfe))
                {
                    if (((this.byteLen < 4) || (this.byteBuffer[2] != 0)) || (this.byteBuffer[3] != 0))
                    {
                        this.encoding = new UnicodeEncoding(false, true);
                        this.CompressBuffer(2);
                        flag = true;
                    }
                    else
                    {
                        this.encoding = new UTF32Encoding(false, true);
                        this.CompressBuffer(4);
                        flag = true;
                    }
                }
                else if (((this.byteLen >= 3) && (this.byteBuffer[0] == 0xef)) && ((this.byteBuffer[1] == 0xbb) && (this.byteBuffer[2] == 0xbf)))
                {
                    this.encoding = Encoding.UTF8;
                    this.CompressBuffer(3);
                    flag = true;
                }
                else if ((((this.byteLen >= 4) && (this.byteBuffer[0] == 0)) && ((this.byteBuffer[1] == 0) && (this.byteBuffer[2] == 0xfe))) && (this.byteBuffer[3] == 0xff))
                {
                    this.encoding = new UTF32Encoding(true, true);
                    this.CompressBuffer(4);
                    flag = true;
                }
                else if (this.byteLen == 2)
                {
                    this._detectEncoding = true;
                }
                if (flag)
                {
                    this.decoder = this.encoding.GetDecoder();
                    this._maxCharsPerBuffer = this.encoding.GetMaxCharCount(this.byteBuffer.Length);
                    this.charBuffer = new char[this._maxCharsPerBuffer];
                }
            }
        }

標簽: ssd

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#中實現(xiàn)自定義事件的代碼演示

下一篇:C# 委托現(xiàn)實范例代碼