C#:打开 文件/文件夹选择框,选取,并获得路径名称

using System;
using System.Windows.Forms;

        //选取文件,并获得路径
        private void button1_Click(object sender, EventArgs e) {
            var f = new OpenFileDialog();
            //f.Multiselect = true; //多选
            if (f.ShowDialog() == DialogResult.OK) {
                String filepath = f.FileName;//G:\新建文件夹\新建文本文档.txt
                String filename = f.SafeFileName;//新建文本文档.txt
                this.textBox1.Text = filename +"   "+filepath;
            }
        }

        //选取文件夹,并获得路径
        private void button2_Click(object sender, EventArgs e) {
            FolderBrowserDialog f = new FolderBrowserDialog();
            if (f.ShowDialog() == DialogResult.OK) {
                String DirPath = f.SelectedPath;
                this.textBox1.Text = DirPath;//G:\新建文件夹
            }
        }

 

你可能感兴趣的:(C#:打开 文件/文件夹选择框,选取,并获得路径名称)