WPF的文件选择与保存

1.引用Windows.Form

引用window.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; }

 

你可能感兴趣的:(WPF)