五种文件夹选择框的实现方法整理 FolderBrowserDialog OpenFileDialog FolderBrowserDialog 支持输入路径的文件夹选择框
本人将网上的文件夹选择框的功能汇总了一下,具体自己见代码
using Microsoft.WindowsAPICodePack.Dialogs; using Microsoft.WindowsAPICodePack.Dialogs.Controls; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CustomFolderBrowserDialog { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "我是系统自带的"; fbd.SelectedPath = @"C:\"; if (fbd.ShowDialog() == DialogResult.OK) { MessageBox.Show(fbd.SelectedPath); } } ////// 文件的,不是文件夹 /// /// /// private void button2_Click(object sender, EventArgs e) { //引用PresentationFramework var openFileDialog = new Microsoft.Win32.OpenFileDialog() { //Filter = "Excel Files (*.xlsx)|*.xlsx" }; var result = openFileDialog.ShowDialog(); if (result == true) { //MessageBox.Show(openFileDialog.FileName); MessageBox.Show(string.Join(Environment.NewLine, openFileDialog.FileNames.ToList())); } } private void button3_Click(object sender, EventArgs e) { FolderDialog fd = new FolderDialog(); if (fd.DisplayDialog() == DialogResult.OK) { MessageBox.Show(fd.Path); } } private void button4_Click(object sender, EventArgs e) { string rootPath = FileDialog.GetSavePath(); MessageBox.Show(rootPath); } /// /// 推荐方法 /// /// /// private void button5_Click(object sender, EventArgs e) { //在VS里打开Package Manager Console后输入Install-Package WindowsAPICodePack-Shell //https://www.nuget.org/packages/WindowsAPICodePack-Shell/ CommonOpenFileDialog dialog = new CommonOpenFileDialog(); dialog.IsFolderPicker = true;//设置为选择文件夹 dialog.Multiselect = true; //文件夹多选 if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { MessageBox.Show(string.Join(Environment.NewLine, dialog.FileNames.ToList())); } } private void button6_Click(object sender, EventArgs e) { //FolderBrowserDialogEx folderBrowserDialogEx1 = new FolderBrowserDialogEx(); //folderBrowserDialogEx1.DirectoryPath = @"C:\"; //设置默认选择路径 //folderBrowserDialogEx1.Description = "我是自定义标题啊"; //folderBrowserDialogEx1.ShowHidden = true; if (folderBrowserDialogEx1.ShowDialog(this) == DialogResult.OK) { MessageBox.Show("选择了:" + folderBrowserDialogEx1.DirectoryPath); } } } }
源码下载:链接: https://pan.baidu.com/s/190AaRBib4tP2jDvJCrwdxw 提取码: spc4 复制这段内容后打开百度网盘手机App,操作更方便哦