WPF的文件选择与保存

xiaoxiao2025-04-27  10

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; }

https://blog.csdn.net/xiecailang/article/details/50770617

转载请注明原文地址: https://www.6miu.com/read-5029252.html

最新回复(0)