WPF的文件选择与保存

1.引用Windows.Form

 

2.打开文件

 

 1 System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
 2             openFileDialog1.InitialDirectory = "c:\\";
 3             openFileDialog1.Filter = "(*.mdb)|*.mdb";
 4             openFileDialog1.FilterIndex = 2;
 5             openFileDialog1.RestoreDirectory = true;
 6             if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 7             {
 8                 //此处做你想做的事 
 9                 this.txt_datasource.Text = openFileDialog1.FileName;
10                 }

 

 

3.保存文件

 1 System.Windows.Forms.SaveFileDialog saveDg = new System.Windows.Forms.SaveFileDialog();
 2             saveDg.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx";
 3             saveDg.FileName = tableName+DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss")+"";
 4             saveDg.AddExtension = true;
 5             saveDg.RestoreDirectory = true;
 6             if (saveDg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 7             {
 8                 //此处做你想做的事
 9                 string filePath = saveDg.FileName;
10                 }

 

4.文件格式过滤

 

 1 //
 2         // 摘要:
 3         //     获取或设置当前文件名筛选器字符串,该字符串决定对话框的“另存为文件类型”或“文件类型”框中出现的选择内容。
 4         //
 5         // 返回结果:
 6         //     对话框中可用的文件筛选选项。
 7         //
 8         // 异常:
 9         //   T:System.ArgumentException:
10         //     Filter 格式无效。
11         [DefaultValue("")]
12         [Localizable(true)]
13         [SRCategoryAttribute("CatBehavior")]
14         [SRDescriptionAttribute("FDfilterDescr")]
15         public string Filter { get; set; }

 

本文转自  https://blog.csdn.net/xiecailang/article/details/50770617?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

posted @ 2020-11-05 11:55  木板  阅读(598)  评论(0)    收藏  举报