views:

87

answers:

4

I'm trying to save the contents of a textbox to a text file using Visual C#. I use the following code:

private void savelog_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog3save.ShowDialog() == DialogResult.OK)
        {
            // create a writer and open the file
            TextWriter tw = new StreamWriter(folderBrowserDialog3save.SelectedPath + "logfile1.txt");
            // write a line of text to the file
            tw.WriteLine(logfiletextbox);
            // close the stream
            tw.Close();
            MessageBox.Show("Saved to " + folderBrowserDialog3save.SelectedPath + "\\logfile.txt", "Saved Log File", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

but I only get the following line of text in the textfile:

System.Windows.Forms.TextBox, Text: 

Followed by only a short portion of what was actually in the textbox, ended with '...'. Why doesn't it write the entire contents of the textbox?

+6  A: 
private void savelog_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog3save.ShowDialog() == DialogResult.OK)
        {
            // create a writer and open the file
            TextWriter tw = new StreamWriter(folderBrowserDialog3save.SelectedPath + "logfile1.txt");
            // write a line of text to the file
            tw.WriteLine(logfiletextbox.Text);
            // close the stream
            tw.Close();
            MessageBox.Show("Saved to " + folderBrowserDialog3save.SelectedPath + "\\logfile.txt", "Saved Log File", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

small explanation: tw.WriteLine accepts object so it doesn't care what do you pass. Internally it calls .ToString. If .ToString is not overriden it just returns type's name. .Text is property with contents of TextBox

Andrey
Oh what a ridiculous mistake...Thanks very much!!
rar
mark this as your answer then, please.
tobsen
+5  A: 

I think what you need is:

tw.WriteLine(logfiletextbox.Text);

if you don't say '.Text' that's what you get

Hope that helps!

Brett
+5  A: 

Using the TextWriter isn't really necessary in this case.

File.WriteAllText(filename, logfiletextbox.Text) 

is simpler. You'd use TextWriter for a file you need to keep open for a longer period of time.

Ed Ropple
Just added the `filename` parameter. Good point, I was just about to write the same thing :)
MartinStettner
Yeah, I always wonder why so few developers know that the `System.IO` Namespace exists, and even less know of such pearls like that one.
Bobby
Whoops. Fail on my part there. Thanks!
Ed Ropple
+1  A: 

Options: When using WriteLine(), note that the content saved to the file is the Text of the TextBox, plus a newline. So, the contents of the file will not match exactly the contents of the TextBox. When would you care about this? If you use the file to read back in a Text property of a text box later, and go through the save->load->save->load...

Your choices to preserve all text (if you using System.IO):

File.WriteAllText( filename, textBox.Text )

File.WriteAllLines( filename, textBox.Lines )

If you insist on using TextWriter, you could use the using wrapper to handle disposing of the Stream, if you need to use complex logic in your write method.

using( TextWriter tw = new ... )
{
    tw.Write( textBox.Text );
}

Consider that IOExceptions may be thrown whenever attempting to access a file for reading or writing. Consider catching an IOException and handling it in your application (keeping the text, displaying to the user that the text could not be saved, suggest choosing a different file, etc).

maxwellb