C# 使用 .NET 3.5 选择文件夹

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

Choosing a folder with .NET 3.5

提问by Wilka

In a C# .NET 3.5 app (a mix of WinForms and WPF) I want to let the user select a folder to import a load of data from. At the moment, it's using System.Windows.Forms.FolderBrowserDialogbut that's a bit lame. Mainly because you can't type the path into it (so you need to map a network drive, instead of typing a UNC path).

在 C# .NET 3.5 应用程序(WinForms 和 WPF 的混合)中,我想让用户选择一个文件夹来从中导入数据负载。目前,它正在使用,System.Windows.Forms.FolderBrowserDialog但这有点蹩脚。主要是因为您无法在其中键入路径(因此您需要映射网络驱动器,而不是键入 UNC 路径)。

I'd like something more like the System.Windows.Forms.OpenFileDialog, but for folders instead of files.

我想要更像System.Windows.Forms.OpenFileDialog, 但用于文件夹而不是文件的东西。

What can I use instead? A WinForms or WPF solution is fine, but I'd prefer not to PInvoke into the Windows API if I can avoid it.

我可以用什么代替?WinForms 或 WPF 解决方案很好,但如果可以避免的话,我不希望 PInvoke 进入 Windows API。

采纳答案by Cheeso

Don't create it yourself! It's been done. You can use FolderBrowserDialogEx- a re-usable derivative of the built-in FolderBrowserDialog. This one allows you to type in a path, even a UNC path. You can also browse for computers or printers with it. Works just like the built-in FBD, but ... better.

不要自己创造!它已经完成了。您可以使用FolderBrowserDialogEx- 内置 FolderBrowserDialog 的可重用派生。这允许您输入路径,甚至是 UNC 路径。您还可以使用它浏览计算机或打印机。就像内置的 FBD 一样工作,但......更好。

Full Source code. Free. MS-Public license.

完整的源代码。自由。MS-公共许可证。

FolderBrowserDialogEx

FolderBrowserDialogEx

Code to use it:

使用它的代码:

var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
dlg1.Description = "Select a folder to extract to:";
dlg1.ShowNewFolderButton = true;
dlg1.ShowEditBox = true;
//dlg1.NewStyle = false;
dlg1.SelectedPath = txtExtractDirectory.Text;
dlg1.ShowFullPathInEditBox = true;
dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;

// Show the FolderBrowserDialog.
DialogResult result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
    txtExtractDirectory.Text = dlg1.SelectedPath;
}

回答by aku

Unfortunately there are no dialogs other than FolderBrowserDialog for folder selection. You need to create this dialog yourself or use PInvoke.

不幸的是,除了 FolderBrowserDialog 之外没有用于文件夹选择的对话框。您需要自己创建此对话框或使用 PInvoke。

回答by OwenP

So far, based on the lack of responses to my identical question, I'd assume the answer is to roll your own dialog from scratch.

到目前为止,基于对我的相同问题没有回应,我认为答案是从头开始滚动你自己的对话。

I've seen things here and there about subclassing the common dialogs from VB6 and I think this might be part of the solution, but I've never seen anything about modifying what the dialog thinks it's selecting. It'd be possible through .NET via PInvoke and some other tricks, but I have yet to see code that does it.

我在这里和那里看到了关于从 VB6 继承通用对话框的事情,我认为这可能是解决方案的一部分,但我从未见过任何关于修改对话框认为它正在选择的内容。可以通过 .NET 通过 PInvoke 和其他一些技巧来实现,但我还没有看到执行此操作的代码。

I knowit's possible and it's not Vista-specific because Visual Studio has done it since VS 2003.

知道这是可能的,而且它不是特定于 Vista 的,因为 Visual Studio 自 VS 2003 以来就已经做到了。

Here's hoping someone answers either yours or mine!

希望有人能回答你或我的!

回答by Alex Essilfie

After hours of searching for a similar solution I found this answerby leetNightShadeto a working solution.

之后寻找一个类似的解决方案的时间,我发现这个答案leetNightShade工作的解决方案

There are three things I believe make this solution much better than all the others.

我相信有三件事使这个解决方案比所有其他解决方案都要好得多。

  1. It is simple to use.It only requires you include two files (which can be combined to one anyway) in your project.
  2. It falls back to the standard FolderBrowserDialogwhen used on XP or older systems.
  3. The author grants permission to use the code for any purpose you deem fit.

    There's no license as such as you are free to take and do with the code what you will.

  1. 使用起来很简单。它只需要您在项目中包含两个文件(无论如何都可以合并为一个)。
  2. 在 XP 或更旧的系统上使用时,它会回退到标准的FolderBrowserDialog
  3. 作者授权将代码用于您认为合适的任何目的。

    没有任何许可证,您可以随意使用代码并随心所欲。

Download the code here.

此处下载代码。