Click here to Skip to main content
14,386,278 members
Rate this:
Please Sign up or sign in to vote.
See more:
this is my code which runs when someone click on save button

Stream save;
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((save = saveFileDialog1.OpenFile()) != null)
                {

                    save.Close();
                }
            }


before save.close() i want to write all the text of a richtextbox in the file which i m saving,how can i do that?
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Just use this
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
  System.IO.File.WriteAllText(saveFileDialog1.FileName,youtTextBox.Text);
}
   
v2
Comments
Sweety Khan 24-Aug-11 7:20am
   
wOw so easy. but it is giving one error that the file cannot be access. it creates the file but there is nothing written in it.
Prerak Patel 24-Aug-11 7:31am
   
Where you are trying to save?
Sweety Khan 24-Aug-11 7:40am
   
and also save dialog appears two times. means first time i give name to file, click on save then save dialog again appears and having detail file name eg C:\Users\Asma\Desktop\filename and i again click on save then error comes.
Prerak Patel 24-Aug-11 7:45am
   
oops sorry, remove the first line.
Sweety Khan 24-Aug-11 7:50am
   
ok now dialog appears one time but same error comes :( that file cannot be access because another process using it
Prerak Patel 24-Aug-11 8:03am
   
Try to mention some new file. I think your previous code could have the handle of that old file. WriteAllText takes care of open-write-close by itself.
Sweety Khan 24-Aug-11 8:09am
   
lOl i got my mistake. i have to first close the save then write text to file like this
save.Close();
File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
Sweety Khan 24-Aug-11 8:09am
   
lOt of thanxs :)
Prerak Patel 24-Aug-11 8:10am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100