wpf 選擇文件夾


兩種方法

1

添加System.Windows.Forms的引用

System.Windows.Forms.FolderBrowserDialog openFileDialog = new System.Windows.Forms.FolderBrowserDialog(); //選擇文件夾
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意,此處一定要手動引入System.Window.Forms空間,否則你如果使用默認的DialogResult會發現沒有OK屬性
{
txb_Path2.Text = openFileDialog.SelectedPath;
}

 

2

在WPF中,使用Microsoft.Win32.OpenFileDialog只能選擇文件,FolderBrowserDialog只能用樹型的方式選擇文件夾,很不好用.

終於找到一個辦法,使用Windows API Code Pack

在VS里打開Package Manager Console后輸入Install-Package WindowsAPICodePack-Shell獲取包后

就可以像這樣打開選擇文件夾Dialog了:

 

using Microsoft.WindowsAPICodePack.Dialogs;

var dlg = new CommonOpenFileDialog();
dlg.IsFolderPicker = true;
dlg.InitialDirectory = currentDirectory;

if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
var folder = dlg.FileName;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM