【问题标题】:WinRT No mapping for the Unicode character exists in the target multi-byte code pageWinRT 目标多字节代码页中不存在 Unicode 字符的映射
【发布时间】:2013-08-14 22:06:57
【问题描述】:

我正在尝试读取我的 Windows 8 应用商店应用程序中的文件。这是我用来实现此目的的代码片段:

        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);
            }
        }

但是,在以下行抛出异常:

string text = dataReader.ReadString(numbytes);

异常信息:

No mapping for the Unicode character exists in the target multi-byte code page.

我是怎么做到的?

【问题讨论】:

  • 不寻常,我不认为 WinRT 仍然处理多字节编码。然而,它确实指向一个未正确编码的文本文件。

标签: file-io windows-8 windows-runtime datareader


【解决方案1】:

试试这个而不是string text = dataReader.ReadString(numbytes):

dataReader.ReadBytes(stream);
string text = Convert.ToBase64String(stream);

【讨论】:

    【解决方案2】:

    我设法使用类似于 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);
            }
    

    有人可以详细说明,为什么我最初的方法不起作用?

    【讨论】:

    • 它读取记事本保存编码ASCII为“?”当char是中文时。我试试Encoding.ASCII.GetString。可以帮忙吗?
    • @lindexi ASCII 编码不支持汉字。更多信息here
    【解决方案3】:

    如果像我一样,在搜索有关 UWP 的相同错误时,这是​​最重要的结果,请参阅以下内容:

    引发错误的代码(不存在 unicode 字符的映射..):

      var storageFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
            using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
            {
                using (var dataReader = new DataReader(stream))
                {
                    await dataReader.LoadAsync((uint)stream.Size);
                    var json = dataReader.ReadString((uint)stream.Size);
                    return JsonConvert.DeserializeObject<T>(json);
                }
            }
    

    我将其更改为使其正常工作

         var storageFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
            using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
            {
                T data = default(T);
                using (StreamReader astream = new StreamReader(stream.AsStreamForRead()))
                using (JsonTextReader reader = new JsonTextReader(astream))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    data = (T)serializer.Deserialize(reader, typeof(T));
                }
                return data;
            }
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 2014-08-05
      • 2014-11-21
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      相关资源
      最近更新 更多