using System.IO;//必须添加命名空间。
窗口加载时要执行的内容:
saveFileDialog1.InitialDirectory = "D\\123.txt";//初始化文件保存的起始位置
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|Word97-2003文档(*.doc)|*.doc";//添加保存的类型
saveFileDialog1.FilterIndex=1;//类型索引
saveFileDialog1.FileName = "*.txt";//文件名称
saveFileDialog1.Title = "保存文件";//窗口标题
以下是按按钮事件执行的内容:
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
string str=richTextBox1.Text;
string[] sp = str.Split('\n');
foreach (var item in sp)
{
sw.WriteLine(item);
}
sw.Close();
}
}