使用SaveFileDialog写入文件的语法是什么

时间:2015-02-18 00:48:21

标签: c# winforms

我正在尝试写入文本文件,我正在使用MSDN中的示例,但我无法弄清楚如何向其中添加文本?​​

private void button1_Click(object sender, System.EventArgs e)
{
     Stream myStream ;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1.FilterIndex = 2 ;
     saveFileDialog1.RestoreDirectory = true ;

     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
 }

我已将// Code to write the stream goes here.更改为以下几项内容,例如:

string thing = "This gets written";

"This gets written";

但那不起作用,我没有其他想法:L

1 个答案:

答案 0 :(得分:0)

using (var myStream = saveFileDialog1.OpenFile())
using (var writer = new StreamWriter(myStream))
{
    writer.WriteLine("File content goes here");
}