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

前端 未结 3 1711
萌比男神i
萌比男神i 2021-01-04 08:15

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 st         


        
3条回答
  •  孤独总比滥情好
    2021-01-04 08:41

    If, like me, this was the top result when search for the same error regarding UWP, see the below:

    The code I had which was throwing the error (no mapping for the unicode character exists..):

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

    What I changed it to so that it works correctly

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

提交回复
热议问题