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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With