C#的文件打开(openFileDialog)与保存(saveFileDialog)对话框使用

介绍

window进行文件操作的时候,需要获取文件路径,文件打开和保存对话框则可以很好的让用户选取打开路径和保存路径。
同样的还有选取工作路径的控件folderBrowserDialog,都可以在工具箱里面拖入窗体。当然你也可以new一个新的。
下面对openFileDialog控件的属性做一些说明,saveFileDialog可以类推,这两个基本相似。

属性说明
openFileDialog1.InitialDirectory设置对话框打开后的初始目录
openFileDialog1.Filter设置对话框对文件格式的筛选,可以根据你的需要自行编写
openFileDialog1.FileName对话框FileName这一栏显示的字符串
openFileDialog1.CheckFileExists检查文件是否存在
openFileDialog1.CheckPathExists检查路径是否存在
openFileDialog1.ShowDialog()显示对话框让用户进行文件选择,返回值为DialogResult枚举类型

使用

  1. 在你的Visual Studio的工具箱里找到你想要的对话框(openFileDialog或是saveFileDialog),拖入你的form窗体中。就会有如下图的效果。
    在这里插入图片描述
  2. 这时候你就可以在你的类里面直接输入他的变量名来进行操作,比如openFileDialog1。名字可能会不一样,使用时自己要注意下,名字都是可以改变的。
  3. 根据你的需要就可以写在按钮的事件里面了!比如,在你的open按钮点击事件里就可以用上面介绍里面写的那些属性来进行初始化,然后使用他的ShowDialog()方法来使它显示
  4. ShowDialog()的返回值是DialogResult的枚举类型,DialogResult.OK表示用户选择了打开按钮,可以作为判断条件。

示例

下面进行一个简单的小栗子帮助你更好的理解。
可以通过open按钮弹出openFileDialog对话框让用户选择txt文件,然后在窗体的textBox里面显示内容,save按钮保存文件,Save as 按钮弹出saveFileDialog对话框让用户另存为新文件。

窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileDilalogTest
{
    public partial class HexRead : Form
    {
        
        public HexRead()
        {
            InitializeComponent();
            initDialog();
        }

        public void initDialog()
        {
            openFileDialog1.InitialDirectory = @"F:\";//设置起始文件夹
            openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";//设置文件筛选类型
            openFileDialog1.FileName = "";//设施初始的文件名为空
            openFileDialog1.CheckFileExists = true;//检查文件是否存在
            openFileDialog1.CheckPathExists = true;//检查路径是否存在

            saveFileDialog1.InitialDirectory = @"F:\";
            saveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.FileName = "文件名";
        }

        private void button_open_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();//显示对话框接返回值
            if(result == DialogResult.OK){
                label2.Text = openFileDialog1.FileName;
               textBox_content.Text = RWStream.ReadFile(openFileDialog1.FileName);
            }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            bool IsOk = RWStream.WriteFile(openFileDialog1.FileName, textBox_content.Text);
            if (IsOk)
                MessageBox.Show("文件已保存!", "提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button_saveas_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                bool IsOk = RWStream.WriteFile(saveFileDialog1.FileName, textBox_content.Text);
                if (IsOk)
                    MessageBox.Show("文件已保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            textBox_content.Text = "";
        }
    }
}

RWStream类代码(里面封装的是读取和保存的方法)

不熟悉StreamReader和StreamWriter的可以去看下C#你一看就会的文件读取与写入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace FileDilalogTest
{
    class RWStream
    {
        public static string ReadFile(string folder)
        {
            string content = "";//返回的结果字符串
            using (StreamReader sr = new StreamReader(folder))
            {
                content = sr.ReadToEnd();//一次性把文本内容读完
            }
            return content;
        }
        public static bool WriteFile(string folder,string content)
        {
            bool flag = true;
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(folder);//创建StreamWriter对象
                sw.WriteLine(content);
                
                
            }
            catch (Exception ex)
            {
                Console.WriteLine("写入流异常:" + ex.ToString());
                flag = false;
            }
            finally
            {
                sw.Close();//确保最后总会关闭流
                Console.WriteLine("写入流关闭");
            }
            return flag;
        }        
    }
}

效果展示

在这里插入图片描述
在这里插入图片描述
---------------------------------------------------------------------
至此恭喜你掌握了openFileDialog和saveFileDialog的基本使用方法!
2020.4.27 🌤

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 的 `SaveFileDialog` 和 `OpenFileDialog` 是用来让用户选择文件对话框控件。下面是它们的基本使用方法: ### SaveFileDialog ```csharp SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; // 设置文件类型过滤器,只显示 txt 文件和所有文件 saveFileDialog1.FilterIndex = 1; // 设置默认选中的文件类型 saveFileDialog1.RestoreDirectory = true; // 记录用户打开的目录路径 if (saveFileDialog1.ShowDialog() == DialogResult.OK) // 显示对话框并判断用户是否点击了确认按钮 { // 获取用户选择的文件路径 string filePath = saveFileDialog1.FileName; // 将数据保存文件中 // ... } ``` ### OpenFileDialog ```csharp OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; // 设置文件类型过滤器,只显示 txt 文件和所有文件 openFileDialog1.FilterIndex = 1; // 设置默认选中的文件类型 openFileDialog1.RestoreDirectory = true; // 记录用户打开的目录路径 if (openFileDialog1.ShowDialog() == DialogResult.OK) // 显示对话框并判断用户是否点击了确认按钮 { // 获取用户选择的文件路径 string filePath = openFileDialog1.FileName; // 读取文件中的数据,并进行处理 // ... } ``` 以上代码中,`Filter` 属性用来设置文件类型过滤器,只显示指定类型的文件。`FilterIndex` 属性用来设置默认选中的文件类型。`RestoreDirectory` 属性用来记录用户打开的目录路径,下次打开对话框时会自动打开该目录。`ShowDialog()` 方法用来显示对话框,并返回一个 `DialogResult` 枚举值,表示用户点击了哪个按钮(OK、Cancel 等)。如果用户点击了 OK 按钮,则可以通过 `FileName` 属性获取用户选择的文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值