While you can use the GetOpenFileName
function and OPENFILENAME
structure to select folders, the OpenFileDialog
control provides a simpler and easier-to-use method. This control provides a dedicated folder selection interface without the need to manage dialog templates.
To use OpenFileDialog
to select folders, consider using the FolderBrowserDialog
class. This class provides a user-friendly interface for browsing and selecting folders. The following code example demonstrates its usage:
using System.Windows.Forms; using System.IO; namespace FolderSelection { public class FolderSelect { public static void Main() { 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, "提示"); } } } } }
When using the FolderBrowserDialog
class in a WPF application, remember to add the following reference:
System.Windows.Forms
QuoteSystem.IO
namespace (for Directory
classes)By using the FolderBrowserDialog
class, developers can easily and efficiently select folders in C# applications.
The above is the detailed content of How to Easily Select Folders in C# Using the FolderBrowserDialog?. For more information, please follow other related articles on the PHP Chinese website!