如何使用 OpenFileDialog 选择文件夹? [英] How to use OpenFileDialog to select a folder?
问题描述
如何使用OpenFileDialog
来选择文件夹?
How to use OpenFileDialog
to select folders?
我打算使用以下项目:https://github.com/scottwis/OpenFileOrFolderDialog
但是,我遇到了一个问题.它使用GetOpenFileName
函数和OPENFILENAME
结构.而OPENFILENAME
有名为templateID
的成员.它是对话框模板的标识符.并且该项目还包含 res1.rc
文件和模板化对话框初始化.但我不知道如何将此文件附加到我的 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
.
您还必须为Directory
类添加using System.IO
这篇关于如何使用 OpenFileDialog 选择文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!