C# 文件夹浏览器对话框,如打开文件对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10990612/
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
Folder browser dialog like open file dialog
提问by Rockstart
Please see the snapshot below. This was taken from "New project creation" workflow in Visual Studio 2008.
请看下面的快照。这取自 Visual Studio 2008 中的“新建项目”工作流。
This window is used for selecting a folder in which the project will be stored. How do I create a similar window in my c# application?
此窗口用于选择将存储项目的文件夹。如何在我的 c# 应用程序中创建一个类似的窗口?


采纳答案by daniel
It is something similar in Office, a dialog which allows to select a folder. The only difference is that the Select folder button is named "OK" instead of "Select folder".
它与 Office 中的类似,一个允许选择文件夹的对话框。唯一的区别是“选择文件夹”按钮被命名为“确定”而不是“选择文件夹”。
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Core.FileDialog fileDialog = app.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker);
fileDialog.InitialFileName = "c:\Temp\"; //something you want
int nres = fileDialog.Show();
if (nres == -1) //ok
{
Microsoft.Office.Core.FileDialogSelectedItems selectedItems = fileDialog.SelectedItems;
string[] selectedFolders = selectedItems.Cast<string>().ToArray();
if (selectedFolders.Length > 0)
{
string selectedFolder = selectedFolders[0];
}
}
Of course, you need to add references to Microsoft.Office.Core (Microsoft Office 14.0 Object Library) and Microsoft.Office.Interop.Excel (Microsoft Excel 14.0 Object Library).
当然,您需要添加对 Microsoft.Office.Core(Microsoft Office 14.0 对象库)和 Microsoft.Office.Interop.Excel(Microsoft Excel 14.0 对象库)的引用。
回答by guppy81
I found a good article about the default FolderBrowserDialog and its limitations: http://www.ssware.com/articles/folderbrowserdialog-unmasked-everything-you-wanted-to-know-about-the-folder-browser-component-from-dotnet-framework.htm
我找到了一篇关于默认 FolderBrowserDialog 及其限制的好文章:http: //www.ssware.com/articles/folderbrowserdialog-unmasked-everything-you-wanted-to-know-about-the-folder-browser-component-from -dotnet-framework.htm
There is a third party compoment "Shell MegaPack" (http://www.ssware.com/megapack.htm) from ssware which offers windows explorer like file and folder browser-controls for WinForms, ASP.net and WPF.
有一个来自 ssware 的第三方组件“Shell MegaPack”(http://www.ssware.com/megapack.htm),它为 WinForms、ASP.net 和 WPF 提供了像文件和文件夹浏览器控件这样的 Windows 资源管理器。
回答by Joe
I modified the code from C# to VB, and my env is VS2015 + Office 2010. My code is slightly different than Daniel's, as some function from Daniel's code supports to only Office 2003/2007
我把代码从C#改成VB,我的环境是VS2015+Office 2010。我的代码和Daniel的略有不同,因为Daniel代码中的一些功能只支持Office 2003/2007
By using a new excel instance, it will be slower than just opening a OpenFileDialog or OpenFolderDialog, but is it way more user friendly. My program is only calling this code once, so trading off the performance for user-friendliness is not a concern in my case.
通过使用新的 excel 实例,它会比打开 OpenFileDialog 或 OpenFolderDialog 慢,但它是否更用户友好。我的程序只调用此代码一次,因此在我的情况下,为了用户友好性而牺牲性能并不是一个问题。
Imports Microsoft.Office
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub Button_select_raw_dir_Click(sender As Object, e As EventArgs) Handles Button_select_raw_dir.Click
Dim raw_app As Excel.Application = New Excel.Application
Dim raw_data_open_folder_dialog As Microsoft.Office.Core.FileDialog
raw_data_open_folder_dialog = raw_app.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker)
raw_data_open_folder_dialog.AllowMultiSelect = False
raw_data_open_folder_dialog.Title = "Please select the raw data's dir "
Dim nres As Integer = raw_data_open_folder_dialog.Show()
Dim sz_SelectedPath As String = Nothing
If nres = -1 Then '-1 means open... lol
For Each selectedItems As Object In raw_data_open_folder_dialog.SelectedItems
sz_SelectedPath = selectedItems.ToString()
Next
TextBox_raw_data_dir.Text = sz_SelectedPath
End If
raw_app.Quit()
ReleaseComObject(raw_app)
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
' Release excel objects to avoid memory leak
Public Sub ReleaseComObject(ByRef obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
MsgBox("Exception! Failed to release com obj, debug your code.")
End Try
End Sub
If you want a C# version, I believe you are smart enough to port it to C# :)
如果你想要一个 C# 版本,我相信你足够聪明,可以将它移植到 C# :)
回答by BimlJake
If you are ok with adding a nuget package, Microsoft.WindowsAPICodePack.Shell has a CommonOpenFileDialogthat can be used in "folder mode" which should match your desired usage.
如果您可以添加 nuget 包,Microsoft.WindowsAPICodePack.Shell 有一个CommonOpenFileDialog可以在“文件夹模式”中使用的程序包,它应该符合您所需的用法。
var directoryDialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
Title = "Select Folder"
};
回答by Willy Kimura
Kindly check out BetterFolderBrowser. It provides just what you need and so much more.
请查看BetterFolderBrowser。它提供了您所需要的以及更多。
BetterFolderBrowseris a .NET component library that was written to help developers provide a better folder-browsing and selection experience to users by employing a similar browser dialog as the standard OpenFileDialogin place of the current FolderBrowserDialogwhich only allows for single-folder selections with its tree-view display format. This allows for a much easier viewing, modification, searching and selection experience using the standard Windows Explorer dialog.
BetterFolderBrowser是一个 .NET 组件库,旨在帮助开发人员通过使用与标准OpenFileDialog类似的浏览器对话框代替当前的FolderBrowserDialog来帮助开发人员为用户提供更好的文件夹浏览和选择体验,后者只允许使用其树视图显示格式。这允许使用标准的 Windows 资源管理器对话框更轻松地查看、修改、搜索和选择体验。

