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

The Archive Base Latest Questions

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

I am trying to save a file using DialogResult and StringBuilder . After making

  • 0

I am trying to save a file using DialogResult and StringBuilder. After making the text, I am calling the following code to save the file:

    if (dr == DialogResult.OK)
    {

        StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);

        sw.Write(sb.ToString());
        sw.Close();
    }

I tried to add the second parameter to StreamWriter as Encoding.UTF8 but since the first argument is a string rather than a Stream, it does not compile it.

How can I convert that string to a stream to be able to pass the second parameter as Encoding?

The reason for this, is that somewhere in my text I have µ but when the file is saved it shows like μ so the µ is getting screwd!

Thanks

  • 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-26T19:55:18+00:00Added an answer on May 26, 2026 at 7:55 pm

    Just wrap it in a FileStream.

    StreamWriter sw = new StreamWriter(
        new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite),
        Encoding.UTF8
    );
    

    If you want to append, use FileMode.Append instead.

    You should also call Dispose() on a try/finally block, or use a using block to dispose the object when it exceeds the using scope:

    using(
        var sw = new StreamWriter(
            new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite),
            Encoding.UTF8
        )
    )
    {
        sw.Write(sb.ToString());
    }
    

    This will properly close and dispose the streams across all exception paths.

    UPDATE:

    As per JinThakur’s comment below, there is a constructor overload for StreamWriter that lets you do this directly:

    var sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8);
    

    The second parameter specifies whether the StreamWriter should append to the file if it exists, rather than truncating it.

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

Sidebar

Related Questions

I'm trying to save the contents of a textbox to a text file using
I am trying to save an NSMutableArray containing text to a plist file using
I'm trying to save a file at path WindowsFormsApplication1\WindowsFormsApplication1\SaveFile but the following code returning
I'm trying to save my file to the following location FileOutputStream fos = new
I am trying to remove a file from path using following code. But my
I downloaed the file using code shown below. Then i am trying to save
I have an asp.net app and I am trying to save a text file
Using Mac OS X API, I'm trying to save a PDF file with a
I am trying to save a file using GetSaveFileName and want to have a
I am trying to save (compress) a .zip file using JclCompression with the JCL

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.