Home > Backend Development > C++ > How Can I Easily Select Folders in C#?

How Can I Easily Select Folders in C#?

Patricia Arquette
Release: 2025-01-09 15:56:42
Original
445 people have browsed it

How Can I Easily Select Folders in C#?

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>
Copy after login

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!

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