在 C# 中打开目录选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/284751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:37:16  来源:igfitidea点击:

Opening a directory chooser in C#

c#wpfwinforms

提问by Dan Blair

I am writing a quick and dirty application that reads all the files from a given directory. I'm currently using the OpenFileDialog to choose a directory and just culling off the file name that it provides. It seems like there should be a way to just choose directories though, but in a quick browsing of MSDN I didn't find it.

我正在编写一个快速而肮脏的应用程序,它读取给定目录中的所有文件。我目前正在使用 OpenFileDialog 来选择一个目录,只是剔除它提供的文件名。似乎应该有一种方法可以只选择目录,但是在快速浏览 MSDN 时我没有找到它。

If you have a way in winforms or more preferably in WPF I'm all ears.

如果您在 winforms 中或更佳地在 WPF 中有办法,我会全力以赴。

采纳答案by Kibbee

You'll want to use a FolderBrowserDialog.

您需要使用FolderBrowserDialog

回答by Geoff

using FORMS = System.Windows.Forms;

var dialog = new System.Windows.Forms.FolderBrowserDialog();
FORMS.DialogResult result = dialog.ShowDialog();
if (result == FORMS.DialogResult.OK)
{
    MessageBox.Show("Result: " + dialog.SelectedPath);
}