[转]winform中 saveFileDialog 和openFileDialog 保存文件的用法
转http://www.cnblogs.com/smile-wei/archive/2012/07/04/2575630.html
saveFileDialog 用法
#region saveFileDialog的实现 { //string localFilePath, fileNameExt, newFileName, FilePath; string localFilePath = String.Empty; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //设置文件类型 saveFileDialog1.Filter = " xls files(*.xls)|*.txt|All files(*.*)|*.*"; //设置文件名称: saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd") + "-" + "资产信息报表.xls"; //设置默认文件类型显示顺序 saveFileDialog1.FilterIndex = 2; //保存对话框是否记忆上次打开的目录 saveFileDialog1.RestoreDirectory = true; //点了保存按钮进入 if (saveFileDialog1.ShowDialog() == DialogResult.OK) { //获得文件路径 localFilePath = saveFileDialog1.FileName.ToString(); //string filname = this.openFileDialog2.FileName; //获取文件名,不带路径 //fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件路径,不带文件名 //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\")); //给文件名前加上时间 //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt; //在文件名里加字符 //saveFileDialog1.FileName.Insert(1,"dameng"); //System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件 //fs输出带文字或图片的文件,就看需求了 } }
openFileDialog 用法:
#region openFileDialog保存的用法 { //string filname = this.openFileDialog2.FileName; //// 设置根在桌面SpecialFolder //fbdSelectFolder.RootFolder = Environment.SpecialFolder.Desktop; //// 设置当前选择的路径 //fbdSelectFolder.SelectedPath = "C:\\"; //// 允许在对话框中包括一个新建目录的按钮 //fbdSelectFolder.ShowNewFolderButton = true; //// 设置对话框的说明信息 //fbdSelectFolder.Description = "请选择输出目录"; //string selectPath = "资产信息报表"; //if (fbdSelectFolder.ShowDialog() == DialogResult.OK) //{ // // 在此添加代码 // selectPath = fbdSelectFolder.SelectedPath; //} } #endregion