Home > Backend Development > C++ > How to Easily Select Folders in C# Using the FolderBrowserDialog?

How to Easily Select Folders in C# Using the FolderBrowserDialog?

DDD
Release: 2025-01-09 16:12:42
Original
918 people have browsed it

How to Easily Select Folders in C# Using the FolderBrowserDialog?

Use the OpenFileDialog control to select a folder

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.

Use FolderBrowserDialog class

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, "提示");
                }
            }
        }
    }
}
Copy after login

WPF application requirements

When using the FolderBrowserDialog class in a WPF application, remember to add the following reference:

  • System.Windows.Forms Quote
  • System.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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template