本人將網上的文件夾選擇框的功能彙總了一下,具體本身見代碼this
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); } } /// <summary> /// 文件的,不是文件夾 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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); } /// <summary> /// 推薦方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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,操做更方便哦spa