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

后端 未结 5 1837
遇见更好的自我
遇见更好的自我 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:39

    This can be used to prompt for any file name.

    private void savelog_Click(object sender, EventArgs e)
    {
        SaveFileDialog dlg = new SaveFileDialog();
        dlg.Filter = "*.txt|*.txt";
        dlg.RestoreDirectory = true;
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            File.WriteAllText(dlg.FileName, textBoxLog.Text);
        }
    }
    

提交回复
热议问题