WPF的文件选择与保存

本文详细介绍了在Windows环境下使用C#进行文件操作的方法,包括如何利用System.Windows.Forms库中的OpenFileDialog和SaveFileDialog类实现文件的打开与保存功能,以及如何设置文件过滤器来限制可选文件类型。

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.youkuaiyun.com/xiecailang/article/details/50770617

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值