Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:55:10+00:00 2026-05-17T01:55:10+00:00

This question have been answered. I recommend sumit_programmers solution below. For now, I’ve removed

  • 0

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);
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:55:11+00:00Added an answer on May 17, 2026 at 1:55 am

    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)
                {
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question have been answered in 2008. End of 2010 now. Any changes? which
This question might have been answered, but I can't find a solution to my
I know that this question must have been asked and answered a million times,
I have been struggling with this question for awhile now, and I haven't reached
I know this question has probably been answered over 200 times and I have
Although this question have been asked and answered before, including on this website, I
I am pretty much sure that this question have been asked and answered several
I know this question was answered, in C# and I have been trying to
I am sure this question has been answered in numerous places but I have
This may have been answered before, but I cannot find a solution that works.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.