选择文件夹
/// <summary>
/// 选择文件夹
/// </summary>
public void SelectFolder()
{CommonOpenFileDialog dialog = new CommonOpenFileDialog("请选择一个文件夹");dialog.IsFolderPicker = true; //选择文件还是文件夹(true:选择文件夹,false:选择文件)if (dialog.ShowDialog() == CommonFileDialogResult.Ok){string path = dialog.FileName;MessageBox.Show($"当前所选文件夹路径为:{path}");}
}
选择文件
/// <summary>
/// 选择文件
/// </summary>
public void SelectFile()
{CommonOpenFileDialog dialog = new CommonOpenFileDialog("请选择一个文件");if (dialog.ShowDialog() == CommonFileDialogResult.Ok){string path = dialog.FileName;MessageBox.Show($"当前所选文件路径为:{path}");}
}
注:需要添加引用using Microsoft.WindowsAPICodePack.Dialogs;
一、C#通过CommonOpenFileDialog创建文件夹更美观
二、通过nuGet加载dll
首先,打开VS2019,通过菜单栏中的“工具”菜单
然后搜索 WindowsAPICodePack,完成安装。