Search code examples
c#windows-store-appsmicrosoft-metrowinrt-xaml

Exception reading *.htm file from local app data (Metro App)


I'm using FileIO.ReadTextAsync() to read an *.htm webpage which I have saved into "ms-appdata:///local", using Utf8 encoding.

But I get a System.ArgumentOutOfRangeException when doing it. Additional information is No mapping for the Unicode character exists in the target multi-byte code page.

Reading an ordinary *.txt file using the same function works fine. What am I doing wrong ?

Edit : Code

async private void Button_Click(object sender, RoutedEventArgs e)
{
StorageFile SF = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/test3.html"));
string html = await FileIO.ReadTextAsync(SF, Windows.Storage.Streams.UnicodeEncoding.Utf8);
}

Solution

  • Change the file encoding using Visual Studio. When I opened the file it had the encoding: "Western European (Windows) - Codepage 1252"

    1. Open the file in Visual Studio
    2. File > Advanced Save Options... >
    3. Change the encoding to "Unicode (UTF-8 with signature) - Codepage 65001"Advanced save options
    4. Save the file

    Credits: Advanced save options in visual studio