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 8783263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:47:06+00:00 2026-06-13T20:47:06+00:00

I have trouble with saving file from Richtextbox to text file. My richtextbox looks

  • 0

I have trouble with saving file from Richtextbox to text file.

My richtextbox looks like this;

ABC    ...
 SDE   ...
KLO    ...

After i saved it looks like this:

ABC ... SDE ... KLO ...

But i want the same like richtextbox line after lines. What did i do wrong?

 if (saveFileDialog2.ShowDialog() == DialogResult.OK)
        {
            StreamWriter sw = File.CreateText(saveFileDialog2.FileName);
            sw.WriteLine(richTextBox1.Text);
            sw.Flush();
            sw.Close();

            //File.WriteAllText(saveFileDialog2.FileName, str);
        }
  • 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-06-13T20:47:08+00:00Added an answer on June 13, 2026 at 8:47 pm

    You are probably getting this because you are trying to save richTextBox1.Text (the whole text) in one line only using the following code

    StreamWriter sw = File.CreateText(saveFileDialog2.FileName);
    sw.WriteLine(richTextBox1.Text);
    sw.Flush();
    sw.Close();
    

    It’s recommended to use sw.WriteLine() on a specific line number in richTextBox1 then move to another line.

    Example

    for (int i = 0; i < richTextBox1.Lines.Length; i++)
    {
        sw.WriteLine(richTextBox1.Lines[i]);
    }
    sw.Flush();
    sw.Close();
    

    Another Solution


    There’s already a built-in function for RichTextBox to save a file with a specific encoding. You may use RichTextBox.SaveFile() for this purpose.

    Example

    RichTextBox.SaveFile(string path, RichTextBoxStreamType);
    

    Where path represents saveFileDialog2.FileName in your code. For RichTextBoxStreamType, it’s best to set it as RichTextBoxStreamType.PlainText as long as you do not use RTF such as Color/Font/Protection/Indent/etc…

    Then, you may read the file again using the following method

    RichTextBox.LoadFile(string path, RichTextBoxStreamType);
    

    NOTICE: If the file is not in RTF and you try to read it in RTF (RichTextBox.LoadFile(string path, RichTextBoxStreamType.RichText);)
    you may encounter formatting errors. In this case, you’ll need to catch the exception and read the file in a Plain or Unicode encoding.

    Example

    RichTextBox _RichTextBox = new RichTextBox();
    try
    {
         _RichTextBox.LoadFile(@"D:\Resources\text.txt", RichTextBoxStreamType.RichText);
    }
    catch (Exception EX)
    {
         if (EX.Message.ToLower().Contains("format is not valid"))
         {
              _RichTextBox.LoadFile(@"D:\Resources\text.txt", RichTextBoxStreamType.PlainText);
         }
    }
    

    Thanks,

    I hope you find this helpful 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble figuring out why a simple division like this one always returns
I'm trying to remove vowels from a text file and am having some trouble.
I have trouble with saving images on sd card. I can see the file
How would you go about saving TRichedit text to an Excel file? I have
I have trouble doing this... You may want to get the same result as
I have trouble shooting at file download problem. In Chrome, the download works fine.
I have trouble launching Glassfish v3 on Mac In Windows, after installation of Glassfish
I have trouble finding way to correctly refactor this code so that there would
I just started C# today, and i have trouble reading an excel file. Here's
I am having trouble saving a dotplot to pdf when this command is done

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.