示例代码如下:
SaveFileDialog sfd = new SaveFileDialog(); //设置文件类型 sfd.Filter = "文本文件(*.txt)|*.txt|Word文件(*.docx)|*.docx"; //设置默认文件类型显示顺序 sfd.FilterIndex = 1; //保存对话框是否记忆上次打开的目录 sfd.RestoreDirectory = true; //点了保存按钮进入 if (sfd.ShowDialog() == DialogResult.OK) { string localFilePath = sfd.FileName; //获得文件路径 using (FileStream fs = new FileStream(localFilePath, FileMode.Create)) { byte[] byt_save = System.Text.Encoding.Default.GetBytes(txtRecvData.Text); fs.Write(byt_save, 0, byt_save.Length); } }