如何使用OpenFileDialog选择文件夹? [英] How to use OpenFileDialog to select a folder?

查看:163
本文介绍了如何使用OpenFileDialog选择文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 OpenFileDialog 选择文件夹?

我将使用以下项目: https://github.com/scottwis/OpenFileOrFolderDialog

然而,我面对一个问题它使用 GetOpenFileName 函数和 OPENFILENAME 结构。而 OPENFILENAME 的成员名为 templateID 。它是对话框模板的标识符。该项目还包含 res1.rc 文件和模板对话框init。但是我不知道如何将这个文件附加到我的C#项目中。

However, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure. And OPENFILENAME has the member named templateID. It's the identifier for dialog template. And the project contains the res1.rc file and the templated dialog init, too. But I couldn't figure out how to attach this file to my C# project.

有没有更好的方法来使用一个 OpenFileDialog 选择文件夹?

Is there a better way to use an OpenFileDialog to select folders?

推荐答案

基本上你需要 FolderBrowserDialog


提示用户选择一个文件夹。此类不能被继承。

Prompts the user to select a folder. This class cannot be inherited.

示例:

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 found: " + files.Length.ToString(), "Message");
    }
}

如果您在 WPF 你必须添加对 System.Windows.Forms 的引用。

If you work in WPF you have to add the reference to System.Windows.Forms.

你还必须添加使用System.IO 目录

这篇关于如何使用OpenFileDialog选择文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆