低调的机器猫 · C# 数字证书 RSA加密解密 加签验签 ...· 4 小时前 · |
老实的橙子 · Day 11 : psycopg2 操作 ...· 4 小时前 · |
温文尔雅的大象 · C++ 序列化和反序列化-腾讯云开发者社区-腾讯云· 1分钟前 · |
刚分手的甘蔗 · C++ XML对象序列化与反序列化_c++ ...· 1分钟前 · |
傲视众生的鞭炮 · boost serialization ...· 57 秒前 · |
旅途中的小熊猫 · trainOptions怎么设置迭代次数 ...· 2 月前 · |
独立的手链 · 在Python中打印pdf文件· 1 年前 · |
文质彬彬的熊猫 · 谁对FastReport Online ...· 1 年前 · |
安静的小熊猫 · powerbi按条件求和-掘金· 1 年前 · |
如何使用
OpenFileDialog
选择文件夹?
我打算使用以下项目: https://github.com/scottwis/OpenFileOrFolderDialog
然而,我面临着一个问题。它使用了
GetOpenFileName
函数和
OPENFILENAME
结构。
OPENFILENAME
有一个名为
templateID
的成员。它是对话框模板的标识符。该项目还包含
res1.rc
文件和模板化对话框初始化。但是我不知道如何将这个文件附加到我的C#项目中。
有没有更好的方式使用
OpenFileDialog
来选择文件夹?
听起来你只是在找 FolderBrowserDialog
奇怪的是,这么多答案/投票,但没有人添加以下代码作为答案:
using (var opnDlg = new OpenFileDialog()) //ANY dialog
//opnDlg.Filter = "Png Files (*.png)|*.png";
//opnDlg.Filter = "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|CSV Files (*.csv)|*.csv"
if (opnDlg.ShowDialog() == DialogResult.OK)
//opnDlg.SelectedPath -- your result
}
为了提醒那些想要避免使用
FolderBrowserDialog
的用户,微软曾经发布了一个名为WindowsAPICodePack的应用程序接口,它有一个名为
CommonOpenFileDialog
的有用的对话框,可以设置为
IsFolderPicker
模式。该应用程序接口可从微软以
NuGet package
形式获得。
这就是我安装和使用
CommonOpenFileDialog
所需的全部内容。(NuGet处理了依赖项)
Install-Package Microsoft.WindowsAPICodePack-Shell
对于include行:
using Microsoft.WindowsAPICodePack.Dialogs;
用法:
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = "C:\\Users";
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
MessageBox.Show("You selected: " + dialog.FileName);
}
有一种使用
OpenFileDialog
的黑客解决方案,其中
ValidateNames
和
CheckFileExists
都被设置为false,并为
FileName
提供了一个模拟值,以指示选择了一个目录。
我之所以说hack,是因为它让用户对如何选择文件夹感到困惑。它们需要在所需的文件夹中,然后只需在文件名显示"Folder Selection“时按Open。
这是基于Denis Stankovski的 Select file or folder from the same dialog 。
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);
// ...
}
这应该是最明显和最直接的方法
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;
}
这是一个纯C#版本,不含nuget,适用于所有版本的.NET (包括.NET核心、.NET 5、WPF、Winforms等)。并使用Windows Vista (和更高版本)的 FOS_PICKFOLDERS 选项的 IFileDialog 界面,所以它有很好的文件夹选择器Windows标准UI。
我还添加了WPF的
Window
类型支持,但这是可选的,可以只删除WPF标记的行。
用法:
var dlg = new FolderPicker();
dlg.InputPath = @"c:\windows\system32";
if (dlg.ShowDialog() == true)
MessageBox.Show(dlg.ResultPath);
}
代码:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows; // for WPF support
using System.Windows.Interop; // for WPF support
public class FolderPicker
public virtual string ResultPath { get; protected set; }
public virtual string ResultName { get; protected set; }
public virtual string InputPath { get; set; }
public virtual bool ForceFileSystem { get; set; }
public virtual string Title { get; set; }
public virtual string OkButtonLabel { get; set; }
public virtual string FileNameLabel { get; set; }
protected virtual int SetOptions(int options)
if (ForceFileSystem)
options |= (int)FOS.FOS_FORCEFILESYSTEM;
return options;
// for WPF support
public bool? ShowDialog(Window owner = null, bool throwOnError = false)
owner ??= Application.Current.MainWindow;
return ShowDialog(owner != null ? new WindowInteropHelper(owner).Handle : IntPtr.Zero, throwOnError);
// for all .NET
public virtual bool? ShowDialog(IntPtr owner, bool throwOnError = false)
var dialog = (IFileOpenDialog)new FileOpenDialog();
if (!string.IsNullOrEmpty(InputPath))
if (CheckHr(SHCreateItemFromParsingName(InputPath, null, typeof(IShellItem).GUID, out var item), throwOnError) != 0)
return null;
dialog.SetFolder(item);
var options = FOS.FOS_PICKFOLDERS;
options = (FOS)SetOptions((int)options);
dialog.SetOptions(options);
if (Title != null)
dialog.SetTitle(Title);
if (OkButtonLabel != null)
dialog.SetOkButtonLabel(OkButtonLabel);
if (FileNameLabel != null)
dialog.SetFileName(FileNameLabel);
if (owner == IntPtr.Zero)
owner = Process.GetCurrentProcess().MainWindowHandle;
if (owner == IntPtr.Zero)
owner = GetDesktopWindow();
var hr = dialog.Show(owner);
if (hr == ERROR_CANCELLED)
return null;
if (CheckHr(hr, throwOnError) != 0)
return null;
if (CheckHr(dialog.GetResult(out var result), throwOnError) != 0)
return null;
if (CheckHr(result.GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, out var path), throwOnError) != 0)
return null;
ResultPath = path;
if (CheckHr(result.GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEEDITING, out path), false) == 0)
ResultName = path;
return true;
private static int CheckHr(int hr, bool throwOnError)
if (hr != 0)
if (throwOnError)
Marshal.ThrowExceptionForHR(hr);
return hr;
[DllImport("shell32")]
private static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IBindCtx pbc, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IShellItem ppv);
[DllImport("user32")]
private static extern IntPtr GetDesktopWindow();
#pragma warning disable IDE1006 // Naming Styles
private const int ERROR_CANCELLED = unchecked((int)0x800704C7);
#pragma warning restore IDE1006 // Naming Styles
[ComImport, Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")] // CLSID_FileOpenDialog
private class FileOpenDialog
[ComImport, Guid("42f85136-db7e-439c-85f1-e4075d135fc8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IFileOpenDialog
[PreserveSig] int Show(IntPtr parent); // IModalWindow
[PreserveSig] int SetFileTypes(); // not fully defined
[PreserveSig] int SetFileTypeIndex(int iFileType);
[PreserveSig] int GetFileTypeIndex(out int piFileType);
[PreserveSig] int Advise(); // not fully defined
[PreserveSig] int Unadvise();
[PreserveSig] int SetOptions(FOS fos);
[PreserveSig] int GetOptions(out FOS pfos);
[PreserveSig] int SetDefaultFolder(IShellItem psi);
[PreserveSig] int SetFolder(IShellItem psi);
[PreserveSig] int GetFolder(out IShellItem ppsi);
[PreserveSig] int GetCurrentSelection(out IShellItem ppsi);
[PreserveSig] int SetFileName([MarshalAs(UnmanagedType.LPWStr)] string pszName);
[PreserveSig] int GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
[PreserveSig] int SetTitle([MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
[PreserveSig] int SetOkButtonLabel([MarshalAs(UnmanagedType.LPWStr)] string pszText);
[PreserveSig] int SetFileNameLabel([MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
[PreserveSig] int GetResult(out IShellItem ppsi);
[PreserveSig] int AddPlace(IShellItem psi, int alignment);
[PreserveSig] int SetDefaultExtension([MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
[PreserveSig] int Close(int hr);
[PreserveSig] int SetClientGuid(); // not fully defined
[PreserveSig] int ClearClientData();
[PreserveSig] int SetFilter([MarshalAs(UnmanagedType.IUnknown)] object pFilter);
[PreserveSig] int GetResults([MarshalAs(UnmanagedType.IUnknown)] out object ppenum);
[PreserveSig] int GetSelectedItems([MarshalAs(UnmanagedType.IUnknown)] out object ppsai);
[ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IShellItem
[PreserveSig] int BindToHandler(); // not fully defined
[PreserveSig] int GetParent(); // not fully defined
[PreserveSig] int GetDisplayName(SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
[PreserveSig] int GetAttributes(); // not fully defined
[PreserveSig] int Compare(); // not fully defined
#pragma warning disable CA1712 // Do not prefix enum values with type name
private enum SIGDN : uint
SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000,
SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
SIGDN_FILESYSPATH = 0x80058000,
SIGDN_NORMALDISPLAY = 0,
SIGDN_PARENTRELATIVE = 0x80080001,
SIGDN_PARENTRELATIVEEDITING = 0x80031001,
SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001,
SIGDN_PARENTRELATIVEPARSING = 0x80018001,
SIGDN_URL = 0x80068000
[Flags]
private enum FOS
FOS_OVERWRITEPROMPT = 0x2,
FOS_STRICTFILETYPES = 0x4,
FOS_NOCHANGEDIR = 0x8,
FOS_PICKFOLDERS = 0x20,
FOS_FORCEFILESYSTEM = 0x40,
FOS_ALLNONSTORAGEITEMS = 0x80,
FOS_NOVALIDATE = 0x100,
FOS_ALLOWMULTISELECT = 0x200,
FOS_PATHMUSTEXIST = 0x800,
FOS_FILEMUSTEXIST = 0x1000,
FOS_CREATEPROMPT = 0x2000,
FOS_SHAREAWARE = 0x4000,
FOS_NOREADONLYRETURN = 0x8000,
FOS_NOTESTFILECREATE = 0x10000,
FOS_HIDEMRUPLACES = 0x20000,
FOS_HIDEPINNEDPLACES = 0x40000,
FOS_NODEREFERENCELINKS = 0x100000,
FOS_OKBUTTONNEEDSINTERACTION = 0x200000,
FOS_DONTADDTORECENT = 0x2000000,
FOS_FORCESHOWHIDDEN = 0x10000000,
温文尔雅的大象 · C++ 序列化和反序列化-腾讯云开发者社区-腾讯云 1分钟前 |
独立的手链 · 在Python中打印pdf文件 1 年前 |
安静的小熊猫 · powerbi按条件求和-掘金 1 年前 |