I want to open text files and load them into a RichTextBox. This has been going fine so far, but now I’m struggling with an encoding issue.
So I used the GetType() method from this StackOverflow page:
How to find out the Encoding of a File? C#
– and it returns “System.Text.UnicodeEncoding”.
My questions now are:
- How do I convert Unicode (I guess that’s what they are, although I haven’t double checked) into UTF8 (and possibly backwards)?
- Can I switch the RichTextBox to display Unicode correctly? The following shows awkward results:
rtb.LoadFile(aFile, RichTextBoxStreamType.PlainText); - How can I define which encoding a SaveFileDialog should use?
Instead of having the RichTextBox load the file from the disk, load it yourself, while specifying the correct encoding. (By the way,
Encoding.Unicodeis just a synonym for “UTF-16 little-endian“.)This will take care of the conversion for you. The string you get is encoded “correctly” (i.e. in the format used internally by .NET), so you can just assign it to the
Textproperty of your RichTextBox.About your third question: The
SaveFileDialogis just a tool that lets the user choose a file name. What you do with the file name (like: save some text into it, or encode some string and then save it) has nothing to do with the SaveFileDialog.