c#OpenFileDialog打开对话框功能

首先添加一个按钮控件
代码

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 WindowsFormsApp8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.InitialDirectory = @"C:\Users\Hab_L\Downloads";
            //对话框初始化路径
            op.Filter = "c#文件(*.cs)|*.cs|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            op.FilterIndex = 2;//默认选择文本文件
            op.DereferenceLinks = false;
            //返回快捷方式的路径而不是快捷方式映射的文件的路径
            op.Title = "打开对话框实例";
            op.RestoreDirectory = true;
            //每次打开都回到InitialDirectory设置的初始路径
            op.ShowHelp = true;//对话框帮助按钮
            op.ShowReadOnly = true;//对话框只读打开的复选框
            op.HelpRequest += new EventHandler(op_HelpRequest);
            //注册帮助按钮的事件

            if(op.ShowDialog()==DialogResult.OK)
            {
                string filePath = op.FileName;//文件路径
                string fileName = op.SafeFileName;//文件名
            }
           
        }

        private void op_HelpRequest(object sender, EventArgs e)
        {
            MessageBox.Show("这是按钮帮助说明的测试");
        }
    }
}

运行结果

c#OpenFileDialog打开对话框功能_第1张图片
运行结果.PNG

你可能感兴趣的:(c#OpenFileDialog打开对话框功能)