C# 如何使用 OpenFileDialog 选择文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11624298/
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-09 18:32:49  来源:igfitidea点击:

How to use OpenFileDialog to select a folder?

c#.netdialogopenfiledialog

提问by Yun

How to use OpenFileDialogto select folders?

如何使用OpenFileDialog选择文件夹?

I was going to use the following project: https://github.com/scottwis/OpenFileOrFolderDialog

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

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

但是,我遇到了一个问题。它使用的GetOpenFileName功能和OPENFILENAME结构。并OPENFILENAME拥有名为 的成员templateID。它是对话框模板的标识符。该项目也包含该res1.rc文件和模板化对话框 init。但我不知道如何将此文件附加到我的 C# 项目中。

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

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

采纳答案by Ionic? Biz?u

Basically you need the FolderBrowserDialogclass:

基本上你需要这个FolderBrowserDialog类:

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

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

Example:

例子:

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");
    }
}

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

如果您在WPF 中工作,则必须添加对System.Windows.Forms.

you also have to add using System.IOfor Directoryclass

你还必须using System.IODirectory课堂添加

回答by Simon Whitehead

Sounds to me like you're just after the FolderBrowserDialog.

在我看来,您只是在FolderBrowserDialog 之后

回答by Andrew

Strange that so much answers/votes, but no one add the following code as an answer:

奇怪的是这么多答案/投票,但没有人添加以下代码作为答案:

using (var fldrDlg = new FolderBrowserDialog())
{ 
    //fldrDlg.Filter = "Png Files (*.png)|*.png";
    //fldrDlg.Filter = "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|CSV Files (*.csv)|*.csv"

    if (fldrDlg.ShowDialog() == DialogResult.OK)
    {
        //fldrDlg.SelectedPath -- your result
    }
}

回答by Joe

As a note for future users who would like to avoid using FolderBrowserDialog, Microsoft once released an API called the WindowsAPICodePack that had a helpful dialog called CommonOpenFileDialog, that could be set into a IsFolderPickermode. The API is available from Microsoft as a NuGet package.

作为对希望避免使用 的未来用户的注意事项FolderBrowserDialog,Microsoft 曾经发布了一个名为 WindowsAPICodePack 的 API,该 API 有一个名为 的有用对话框CommonOpenFileDialog,可以将其设置为一种IsFolderPicker模式。该 API 可作为NuGet 包从 Microsoft 获得。

This is all I needed to install and use the CommonOpenFileDialog. (NuGet handled the dependencies)

这就是我安装和使用CommonOpenFileDialog. (NuGet 处理依赖项)

Install-Package Microsoft.WindowsAPICodePack-Shell

For the include line:

对于包含行:

using Microsoft.WindowsAPICodePack.Dialogs;

Usage:

用法:

CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = "C:\Users";
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
    MessageBox.Show("You selected: " + dialog.FileName);
}

回答by Daniel Ballinger

There is a hackish solution using OpenFileDialogwhere ValidateNamesand CheckFileExistsare both set to false and FileNameis given a mock value to indicate that a directory is selected.

有一个hackish 解决方案,使用OpenFileDialogwhereValidateNamesCheckFileExists都设置为 false 并FileName给出一个模拟值以指示选择了一个目录。

I say hack because it is confusing to users about how to select a folder. They need to be in the desired folder and then just press Open while file name says "Folder Selection."

我说 hack 是因为它让用户对如何选择文件夹感到困惑。他们需要位于所需的文件夹中,然后在文件名显示“文件夹选择”时按“打开”。

C# Folder selection dialog

C# 文件夹选择对话框

This is based on Select file or folder from the same dialogby Denis Stankovski.

这是基于Denis Stankovski从同一对话框中选择文件或文件夹

OpenFileDialog folderBrowser = new OpenFileDialog();
// Set validate names and check file exists to false otherwise windows will
// not let you select "Folder Selection."
folderBrowser.ValidateNames = false;
folderBrowser.CheckFileExists = false;
folderBrowser.CheckPathExists = true;
// Always default to Folder Selection.
folderBrowser.FileName = "Folder Selection.";
if (folderBrowser.ShowDialog() == DialogResult.OK)
{
    string folderPath = Path.GetDirectoryName(folderBrowser.FileName);
    // ...
}

回答by Ben Keene

Here is another solution, that has all the source available in a single, simple ZIP file.

这是另一种解决方案,它在一个简单的 ZIP 文件中包含所有可用的源。

It presents the OpenFileDialog with additional windows flags that makes it work like the Windows 7+ Folder Selection dialog.

它为 OpenFileDialog 提供了额外的窗口标志,使其像 Windows 7+ 文件夹选择对话框一样工作。

Per the website, it is public domain: "There's no license as such as you are free to take and do with the code what you will."

根据该网站,它是公共领域:“没有任何许可证可以让您随意使用和使用代码。”

Archive.org links:

Archive.org 链接:

回答by C. Augusto Proiete

Take a look at the Ookii Dialogslibraries which has an implementation of a folder browser dialog for Windows Forms and WPF respectively.

看看Ookii Dialogs库,它分别为 Windows 窗体和 WPF 实现了文件夹浏览器对话框。

enter image description here

在此处输入图片说明

Ookii.Dialogs.WinForms

https://github.com/augustoproiete/ookii-dialogs-winforms

Ookii.Dialogs.WinForms

https://github.com/augustoproiete/ookii-dialogs-winforms



Ookii.Dialogs.Wpf

https://github.com/augustoproiete/ookii-dialogs-wpf

Ookii.Dialogs.Wpf

https://github.com/augustoproiete/ookii-dialogs-wpf

回答by AHM

this should be the most obvious and straight forward way

这应该是最明显和直接的方式

using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{

   System.Windows.Forms.DialogResult result = dialog.ShowDialog();

   if(result == System.Windows.Forms.DialogResult.OK)
   {
      selectedFolder = dialog.SelectedPath;
   }

}