Visual C# - Write contents of a textbox to a .txt file

后端 未结 5 1835
遇见更好的自我
遇见更好的自我 2020-12-31 11:28

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         


        
5条回答
  •  半阙折子戏
    2020-12-31 11:46

    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

提交回复
热议问题