0

Trying to import PlainText file with English characters using a RichTextBox in C# with UWP and VS 2017. Imports fine except all the characters are Chinese. I have to use a StorageFile class for the file because that's the only one that works with UWP file privacy issues. I tried all TexSetOptions with no success and can't find a way to specify format in either the stream or rtb. Here's the code:

       StorageFile file = await StorageFile.GetFileFromPathAsync(filePath));
       IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

       /* NOTE: RichTextBox (Name="editor") is defined in Xaml */

       editor.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.ApplyRtfDocumentDefaults, stream);
  • I don't know much about UWP, but this screams "encoding issue". What encoding does your file have and what encoding does editor.Document.LoadFromStream expect? – Heinzi Apr 17 '20 at 16:22
  • maybe this? https://stackoverflow.com/questions/3825390/effective-way-to-find-any-files-encoding – swcraft Apr 17 '20 at 16:57
  • 1
    Ah, that fun moment when a programmer discovers there's no such thing as "plain text"... – Nyerguds Apr 17 '20 at 17:25
  • Yea...that's the whole problem. Seems like the UWP version of RichTextBox has eliminated a whole bunch of properties and methods. Using the IRandomAccessStream or rtb, I can't find any way to determine what kind of format it's using or expects? – michaelm700 Apr 17 '20 at 18:42

2 Answers2

0

As noted in the comments, this is due to an encoding mismatch. The API expects UTF-16 but you have UTF-8 (or maybe ASCII). Consider using FileIO.ReadTextAsync instead. This should auto-detect the encoding, or if it doesn't there is an overload where you can specify it directly.

Note that if you have a file encoded with an ANSI codepage (not any flavour of Unicode) you'll need to convert it first (check other SO posts).

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • Thanks Peter, works great! I still can't get it to work with RichTextBox (weird, keeps telling me it can't convert a RichTextBox to an IRichTextBox8 which I can find no documentation of). But a plain ole TextBox has the functionality I need. – michaelm700 Apr 18 '20 at 15:19
0

The UWP RichTextBox standard is random access unicode, so just had to adjust the file stream to match.

        string x = await FileIO.ReadTextAsync(file);
        byte[] bytes = System.Text.Encoding.Unicode.GetBytes(x);
        InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
        await randomAccessStream.WriteAsync(bytes.AsBuffer());
        IRandomAccessStream stream2 = randomAccessStream;  //await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        editor.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.ApplyRtfDocumentDefaults, stream2);