简体   繁体   中英

C#: Load *.txt to RichTextBox and convert into UTF8

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.Unicode is just a synonym for " UTF-16 little-endian ".)

string myText = File.ReadAllText(myFilePath, Encoding.Unicode);

This will take care of the conversion for you. The string you get is encoded "correctly" (ie in the format used internally by .NET), so you can just assign it to the Text property of your RichTextBox.


About your third question: The SaveFileDialog is 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.

The SaveFileDialog just allow you to choose the path where the file will be saved. It doesn't save it for you..

Use Encoding class to convert from an encoding to another.

And read this article for some example on how to convert and write it to a file.

You can also use: richTextBox.LoadFile(filePath, RichTextBoxStreamType.UnicodePlainText);

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