windows 8 读取文件 中文乱码问题
windows 8 的应用程序在读取txt文件的时候,尤其是读取中文txt文件的时候,由于txt文件的默认编码是ANSI,所以经常会出现乱码问题,
今天看了一下,发现解决这个问题还是会涉及到编码的问题。
直接上代码
1 FileOpenPicker picker = new FileOpenPicker();
2 picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
3
4 picker.FileTypeFilter.Add(".txt");
5
6 var file = await picker.PickSingleFileAsync();
7 IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
8 StreamReader sr = new StreamReader(stream.AsStream(), System.Text.Encoding.GetEncoding("GB2312"));
9
10 var code = sr.CurrentEncoding;
11 var str = sr.ReadToEnd();
2 picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
3
4 picker.FileTypeFilter.Add(".txt");
5
6 var file = await picker.PickSingleFileAsync();
7 IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
8 StreamReader sr = new StreamReader(stream.AsStream(), System.Text.Encoding.GetEncoding("GB2312"));
9
10 var code = sr.CurrentEncoding;
11 var str = sr.ReadToEnd();
将ANSI编码转换成"GB2312"然后进行编码就可以解决读取文件中文乱码问题