Challenge of selecting folder using OpenFileDialog
Some projects try to use the OpenFileOrFolderDialog
, GetOpenFileName
and OPENFILENAME
structures to select folders. However, integrating the necessary res1.rc
files and dialog templates into a C# project can be complicated.
Simpler alternative: FolderBrowserDialog
To simplify the folder selection process, the FolderBrowserDialog
class is ideal. It provides an intuitive user interface and simplifies the process.
Use FolderBrowserDialog
<code class="language-csharp">using System.Windows.Forms; using System.IO; using (var fbd = new FolderBrowserDialog()) { DialogResult result = fbd.ShowDialog(); if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) { string[] files = Directory.GetFiles(fbd.SelectedPath); MessageBox.Show("找到的文件数量: " + files.Length, "提示"); } }</code>
For WPF projects, you need to add a reference to System.Windows.Forms
. Additionally, System.IO
is also required for the Directory
class.
The above is the detailed content of How Can I Easily Select Folders in C#?. For more information, please follow other related articles on the PHP Chinese website!