简体   繁体   中英

No mapping for the Unicode character exists in the target multi-byte code page on Windows App Save

I have a UWP app that I am deploying as an AppPackage, and I am seeing an off error when I attempt to read/save a file in the LocalState folder. It does not happen everytime, so I am thinking the file is locked during the process at some point. The erroe is

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

And the code that throws the error

private const string SETTINGS_FILENAME = "settings.json";
private static readonly StorageFolder _settingsFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
public async static Task<ConfigWrapper> LoadSettings()
{
    try
    {
        StorageFile sf = await _settingsFolder.GetFileAsync(SETTINGS_FILENAME);
        if (sf == null) return null;

        string content = await FileIO.ReadTextAsync(sf, Windows.Storage.Streams.UnicodeEncoding.Utf8);
        return JsonConvert.DeserializeObject<ConfigWrapper>(content);
    }
    catch (Exception e)
    {
        DiagnosticsClient.TrackException(e);
        return null;
    }
}

public async static Task<bool> SaveSettings(ConfigWrapper data)
{
    try
    {
        StorageFile file = await _settingsFolder.CreateFileAsync(SETTINGS_FILENAME, CreationCollisionOption.ReplaceExisting);
        string content = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { });
        await FileIO.WriteTextAsync(file, content, Windows.Storage.Streams.UnicodeEncoding.Utf8);
        return true;
    }
    catch (Exception e)
    {
        DiagnosticsClient.TrackException(e);
        return false;
    }
}

I am making sure to save and read the file in UTF-8, so I am not sure why there would be an encoding error. Is there something I need to do (lock the file for instance) when I save/read?

Many applications save data in a proprietary format. The gibberish you provided is probably such.

Such applications often have a way to "write" or "export" or "save as" the data. Look for such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM