[英]Saving files with name read from text box. C#
I have created some code that saves my work to a text file, I was just wondering is there some way so that when I click 'Save' I can read in the text from richTextBox1
and set it as the default file name, still with the 'txt' default file extension. 我创建了一些代码,将我的工作保存到文本文件中,我只是想知道是否有某种方法,以便当我单击“保存”时,我可以从
richTextBox1
读取文本并将其设置为默认文件名,仍然使用'txt'默认文件扩展名。
eg When I click 'save' the folder dialog comes up and asks you to name your file, just as it would if you were using Word for example, I want that box to already have the text from my richTextBox1 in. 例如,当我单击“保存”时,出现文件夹对话框,并要求您命名文件,就像您使用Word一样,我希望该框已经包含来自richTextBox1的文本。
Thanks. 谢谢。
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "C:\\To-Do-List";
save.Filter = "Text Files (*.txt)|*.txt";
save.DefaultExt = ".txt";
DialogResult result = save.ShowDialog();
if (result == DialogResult.OK)
{
using (StreamWriter SW = new StreamWriter(save.FileName))
{
SW.WriteLine(richTextBox1.Text);
SW.WriteLine(richTextBox2.Text);
SW.WriteLine(richTextBox3.Text);
SW.WriteLine(richTextBox4.Text);
SW.WriteLine(richTextBox5.Text);
SW.Close();
}
}
Just set the FileName
property on your SaveFileDialog
. 只需在
SaveFileDialog
上设置FileName
属性。 Add 加
save.FileName = String.Format("{0}.txt", richTextBox1.Text);
Before you call ShowDialog
. 在调用
ShowDialog
之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.