本文介绍了WinRT目标多字节代码页中不存在用于Unicode字符的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在我的Windows 8商店应用程序中读取一个文件。这里是我用来实现这一点的代码片段:$ p $ $ $ $ c $ if $($!= null)
{
var stream =等待file.OpenAsync(FileAccessMode.Read);
var size = stream.Size;
using(var inputStream = stream.GetInputStreamAt(0))
{
DataReader dataReader = new DataReader(inputStream);
uint numbytes =等待dataReader.LoadAsync((uint)size);
string text = dataReader.ReadString(numbytes);
$ b
但是, p>
string text = dataReader.ReadString(numbytes);
执行讯息:
目标多字节代码页中不存在用于Unicode字符的映射。
我如何得到这个?
if (file!= null)
{
IBuffer buffer =等待FileIO.ReadBufferAsync(file);
DataReader reader = DataReader.FromBuffer(buffer);
byte [] fileContent = new byte [reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
string text = Encoding.UTF8.GetString(fileContent,0,fileContent.Length);
$ b 有人请详细说明,为什么我的初始方法不起作用? p>
I am trying to read a file in my Windows 8 Store App. Here is a fragment of code I use to achieve this:
if(file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
var size = stream.Size;
using(var inputStream = stream.GetInputStreamAt(0))
{
DataReader dataReader = new DataReader(inputStream);
uint numbytes = await dataReader.LoadAsync((uint)size);
string text = dataReader.ReadString(numbytes);
}
}
However, an exeption is thrown at line:
string text = dataReader.ReadString(numbytes);
Exeption message:
No mapping for the Unicode character exists in the target multi-byte code page.
How do I get by this?
解决方案 I managed to read file correctly using similar approach to suggested by duDE:
if(file != null)
{
IBuffer buffer = await FileIO.ReadBufferAsync(file);
DataReader reader = DataReader.FromBuffer(buffer);
byte[] fileContent = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
string text = Encoding.UTF8.GetString(fileContent, 0, fileContent.Length);
}
Can somebody please elaborate, why my initial approach didn't work?
这篇关于WinRT目标多字节代码页中不存在用于Unicode字符的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!