Home > Backend Development > C++ > How to Select a Folder Efficiently in C# Using FolderBrowserDialog?

How to Select a Folder Efficiently in C# Using FolderBrowserDialog?

Susan Sarandon
Release: 2025-01-09 16:01:42
Original
628 people have browsed it

How to Select a Folder Efficiently in C# Using FolderBrowserDialog?

Use OpenFileDialog more efficiently to select folders

While the OpenFileOrFolderDialog project using the GetOpenFileName function is useful, it requires the attachment of a res1.rc file and dialog initialization, which can cause problems.

Another way to select a folder using OpenFileDialog is to use the FolderBrowserDialog class. This dialog box provides an easy-to-use folder selection interface.

Use FolderBrowserDialog:

<code class="language-csharp">using(var fbd = new FolderBrowserDialog())
{
    DialogResult result = fbd.ShowDialog();

    if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
    {
        string[] files = Directory.GetFiles(fbd.SelectedPath);

        System.Windows.Forms.MessageBox.Show("找到的文件数量: " + files.Length.ToString(), "消息");
    }
}</code>
Copy after login

Key points:

  • For WPF applications, you need to add references to System.Windows.Forms and System.IO for the FolderBrowserDialog and Directory classes respectively.
  • Use the DialogResult.OK status to verify successful folder selection.
  • The SelectedPath property provides access to the selected folder path.

The above is the detailed content of How to Select a Folder Efficiently in C# Using FolderBrowserDialog?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template