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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:29:27+00:00 2026-05-14T15:29:27+00:00

I am having an issue with SaveFileDialog for some reason. All I want to

  • 0

I am having an issue with SaveFileDialog for some reason. All I want to do is extract data from a text box line by line and write it to a text file, then save that text file to the desktop. The first bit of code works fine (though it doesn’t save to the desktop). The second bit of code is what I want to use, but when it creates the text file the text file is empty. What did I do wrong in my second bit of code?

This code works, but it does not save to the desktop and it isn’t as nice as the second code.

//When the Save button is clicked the contents of the text box will be written to a text file.
private void saveButton_Click(object sender, EventArgs e)
{
    int textBoxLines = textBox.Lines.Count();
    if (File.Exists(saveFile))
    {
        result = MessageBox.Show("The file " + saveFile + " already exists.\r\nDo you want to replace it?", "File Already Exists!", MessageBoxButtons.YesNo);

        if (result == DialogResult.Yes)
        {
            TextWriter tw1 = new StreamWriter(saveFile);
            for (int i = 0; i < textBoxLines; i++)
            {
                tw1.WriteLine(textBox.Lines.GetValue(i));
            }

            tw1.Close();

        }

        if (result == DialogResult.No)
        {
            MessageBox.Show("Please move or rename existing " + saveFile + "\r\nBefore attempting to save again.", "Message");
        }
    }

    else
    {
        TextWriter tw2 = new StreamWriter(saveFile);
        for (int i = 0; i < textBoxLines; i++)
        {
            tw2.WriteLine(textBox.Lines.GetValue(i));
        }

        tw2.Close();

    }

}

This code does not work, but it is what I want to use.

//When the Save button is clicked the contents of the text box will be written to a text file.
private void saveButton_Click(object sender, EventArgs e)
{
    int textBoxLines = textBox.Lines.Count();

    Stream saveStream;
    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    saveDialog.FilterIndex = 2;
    saveDialog.RestoreDirectory = true;
    saveDialog.FileName = (saveFile);
    saveDialog.InitialDirectory = (Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

    if (saveDialog.ShowDialog() == DialogResult.OK)
    {
        if ((saveStream = saveDialog.OpenFile()) != null)
        {
            StreamWriter tw = new StreamWriter(saveStream);
            for (int i = 0; i < textBoxLines; i++)
            {
                tw.WriteLine(textBox.Lines.GetValue(i));
            }

            saveStream.Close();
        }
    }
}
  • 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-14T15:29:27+00:00Added an answer on May 14, 2026 at 3:29 pm

    You haven’t got a tw.Close() after writing the contents. The buffer won’t be flushed.

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

Sidebar

Related Questions

I'm having an issue with my regex. I want to capture <% some stuff
I am having issue when sending data from Service to Activity through Notification ,
Having issue with displaying the Data from a second Activity. when I run the
Im having issue with mail sending In web.config file <mailSettings> <smtp from=xxxx@companyname.com> <network host=mail.authsmtp.com
Iam work on full text search build out. Im having issue on how to
I am having issue with Google line chart API. When I reload the div,
I am having issue with opencv's Sobel edge detector. From its documentation it seems
Having some issue with my function call please. I have a situation that I
Having issue with my navigation. How can I prevent it? My CSS file looks
I am having issue with loading a html textarea via ajax, essentially the text

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.