您的位置:首页 > 其它

WPF的文件选择与保存

2016-03-01 10:32 363 查看

1.引用Windows.Form



2.打开文件

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


3.保存文件

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


4.文件格式过滤

//
// 摘要:
//     获取或设置当前文件名筛选器字符串,该字符串决定对话框的“另存为文件类型”或“文件类型”框中出现的选择内容。
//
// 返回结果:
//     对话框中可用的文件筛选选项。
//
// 异常:
//   T:System.ArgumentException:
//     Filter 格式无效。
[DefaultValue("")]
[Localizable(true)]
[SRCategoryAttribute("CatBehavior")]
[SRDescriptionAttribute("FDfilterDescr")]
public string Filter { get; set; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: