Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

winforms - Open file in rich text box with C#

This question have been answered. I recommend sumit_programmers solution below. For now, I've removed my code, thinking it's more confusing than helpful. When I've developed it a bit further, perhaps I'll post my code here, with some comments.

You may also be interested in the answer to the question Save text from rich text box with C#. There is an answer that reminds of the accepted answer to this question. The code should work, but it's written by me, so there may be some errors or missing information.


Update: I have improved the code a bit (at least I think so). "Encoding.Default" seems to work with most common encodings, like ANSI. If the encoding is UTF-8 without byte order mark (BOM), it seems "Encoding.Default" doesn't work, though. For more information, go to informit.com/guides. Here's the code I'm using right now:

private void fileOpen_Click(object sender, EventArgs e)
{
  using (OpenFileDialog dlgOpen = new OpenFileDialog())
  {
    try
    {
      // Available file extensions
      dlgOpen.Filter = "All files(*.*)|*.*";
      // Initial directory
      dlgOpen.InitialDirectory = "D:";
      // OpenFileDialog title
      dlgOpen.Title = "Open";
      // Show OpenFileDialog box
      if (dlgOpen.ShowDialog() == DialogResult.OK)
      {
        // Create new StreamReader
        StreamReader sr = new StreamReader(dlgOpen.FileName, Encoding.Default);
        // Get all text from the file
        string str = sr.ReadToEnd();
        // Close the StreamReader
        sr.Close();
        // Show the text in the rich textbox rtbMain
        rtbMain.Text = str;
      }
    }
    catch (Exception errorMsg)
    {
      MessageBox.Show(errorMsg.Message);
    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you are getting that error as you are trying to access file that can't be loaded in Rich Text Box. If you want to load a .rtf file you need to add this line

richTextBox1.LoadFile(dlg.FileName, RichTextBoxStreamType.RichText);

and if you want to load .txt file, you need to add this

richTextBox1.LoadFile(dlg.FileName, RichTextBoxStreamType.PlainText);

Sample Code:

 using (OpenFileDialog ofd = new OpenFileDialog())
        {
            try
            {
                ofd.Filter = "Text files (*.txt)|*.txt|RTF files (*.rtf)|*.rtf";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (Path.GetExtension(ofd.FileName) == ".rtf")
                    {
                        richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText);
                    }
                    if (Path.GetExtension(ofd.FileName) == ".txt")
                    {
                        richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                    }

                }
            }
            catch (Exception ex)
            {
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...