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窗体中。就会有如下图的效果。
    C#的文件打开(openFileDialog)与保存(saveFileDialog)对话框使用_第1张图片
  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;
        }        
    }
}

效果展示

C#的文件打开(openFileDialog)与保存(saveFileDialog)对话框使用_第2张图片
C#的文件打开(openFileDialog)与保存(saveFileDialog)对话框使用_第3张图片
---------------------------------------------------------------------
至此恭喜你掌握了openFileDialog和saveFileDialog的基本使用方法!
2020.4.27

你可能感兴趣的:(C#笔记)