/// <summary> /// 向用户显示 FolderBrowser 的对话框 /// </summary> /// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param> /// <returns></returns> public DialogResult ShowDialog(IWin32Window owner) { IntPtr handle = owner?.Handle ?? GetActiveWindow(); IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; //如果选择路径不为空,则设置默认文件夹 if (!string.IsNullOrEmpty(DirectoryPath)) { IntPtr idl; uint atts = 0; if (SHILCreateFromPath(DirectoryPath, out idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } } } //自定义标题 if (!string.IsNullOrEmpty(Description)) { dialog.SetTitle(Description); } //是否显示隐藏文件 if (ShowHidden) { //本人扩展的项目 dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM | FOS.FOS_FORCESHOWHIDDEN); } else { //原作者代码 dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); } uint hr = dialog.Show(handle); if (hr == ERROR_CANCELLED) { return(DialogResult.Cancel); } if (hr != 0) { return(DialogResult.Abort); } dialog.GetResult(out item); string path; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); DirectoryPath = path; return(DialogResult.OK); } finally { Marshal.ReleaseComObject(dialog); } }
private void Configure(IFileOpenDialog dialog) { dialog.SetOptions(CreateOptions()); if (!string.IsNullOrEmpty(InitialDirectory)) { var result = NativeMethods.SHCreateItemFromParsingName(InitialDirectory, IntPtr.Zero, typeof(IShellItem).GUID, out var item); if (result != NativeMethods.S_OK) { throw new Win32Exception(result); } if (item != null) { dialog.SetFolder(item); } } if (Title != null) { dialog.SetTitle(Title); } if (OkButtonLabel != null) { dialog.SetOkButtonLabel(OkButtonLabel); } }
private void SetOptions([NotNull] IFileOpenDialog dialog) { if (dialog == null) { throw new ArgumentNullException(nameof(dialog)); } dialog.SetOptions(GetDialogOptions()); }
private static String ShowDialogInner(IFileOpenDialog dialog, IntPtr parentHWnd, String title, String initialDirectory) #endif { //IFileDialog ifd = dialog; FileOpenOptions flags = FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.PickFolders | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { #if NETCOREAPP3_1_OR_GREATER IShellItem2?initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); #else IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); #endif if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } // HResult hr = dialog.Show(parentHWnd); if (hr.ValidateDialogShowHResult()) { dialog.GetResults(out IShellItemArray resultsArray); #if NETCOREAPP3_1_OR_GREATER IReadOnlyList <String?> fileNames = Utility.GetFileNames(resultsArray); #else IReadOnlyList <String> fileNames = Utility.GetFileNames(resultsArray); #endif if (fileNames.Count == 0) { return(null); } else { return(fileNames[0]); } } else { // User cancelled. return(null); } }
public DialogResult ShowDialog(IWin32Window owner) { IntPtr hwndOwner = owner != null ? owner.Handle : GetActiveWindow(); try { if (Environment.OSVersion.Version.Major >= 6) { IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; if (!string.IsNullOrEmpty(DirectoryPath)) { IntPtr idl; uint atts = 0; if (SHILCreateFromPath(DirectoryPath, out idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } } } dialog.SetTitle(Title); dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); uint hr = dialog.Show(hwndOwner); if (hr == ERROR_CANCELLED) { return(DialogResult.Cancel); } if (hr != 0) { return(DialogResult.Abort); } dialog.GetResult(out item); string path; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); DirectoryPath = path; return(DialogResult.OK); } finally { Marshal.ReleaseComObject(dialog); } } else { return(showDefaultDialogEx(owner)); } } catch (Exception ex) { return(showDefaultDialog(owner)); } }
private static IReadOnlyList <String> ShowDialogInner(IFileOpenDialog dialog, IntPtr parentHWnd, String title, String initialDirectory, String defaultFileName, IReadOnlyCollection <Filter> filters, Int32 selectedFilterZeroBasedIndex, FileOpenOptions flags) #endif { flags = flags | FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { #if NETCOREAPP3_1_OR_GREATER IShellItem2?initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); #else IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); #endif if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (defaultFileName != null) { dialog.SetFileName(defaultFileName); } Utility.SetFilters(dialog, filters, selectedFilterZeroBasedIndex); // HResult hr = dialog.Show(parentHWnd); if (hr.ValidateDialogShowHResult()) { dialog.GetResults(out IShellItemArray resultsArray); IReadOnlyList <String> fileNames = Utility.GetFileNames(resultsArray); return(fileNames); } else { // User cancelled. return(null); } }
private static String[] ShowDialogInner(IFileOpenDialog dialog, IntPtr parentWindowHandle, String title, String initialDirectory, String defaultFileName, IReadOnlyCollection <Filter> filters, Int32 selectedFilterZeroBasedIndex, FileOpenOptions flags) { flags = flags | FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (defaultFileName != null) { dialog.SetFileName(defaultFileName); } Utility.SetFilters(dialog, filters, selectedFilterZeroBasedIndex); HResult result = dialog.Show(parentWindowHandle); if (result == HResult.Ok) { IShellItemArray resultsArray; dialog.GetResults(out resultsArray); String[] fileNames = Utility.GetFileNames(resultsArray); return(fileNames); } else if (result == HResult_Win32_Canceled) { // Cancelled by user. return(null); } else { UInt32 win32ErrorCode = Utility.Win32ErrorFromHResult((UInt32)result); throw new Win32Exception(error: (Int32)win32ErrorCode); } }
/// <summary> /// Displays a file/folder selection dialog. /// </summary> /// <param name="owner">The parent window.</param> public bool?ShowDialog(Window owner) { try { dialog.SetOptions(options); dialog.Show(new WindowInteropHelper(owner).Handle); return(true); } catch (Exception ex) { return(ex.HResult == unchecked ((int)0x800704C7) ? (bool?)false : null); } }
public bool?ShowDialog(IWin32Window owner) { IntPtr hwndOwner = owner != null ? owner.Handle : GetActiveWindow(); IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; string path; if (!string.IsNullOrEmpty(DirectoryPath)) { IntPtr idl; uint atts = 0; if ((SHILCreateFromPath(DirectoryPath, out idl, ref atts) == 0) && (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0)) { dialog.SetFolder(item); } } dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); uint hr = dialog.Show(hwndOwner); if (hr == ERROR_CANCELLED) { return(false); } if (hr != 0) { return(null); } dialog.GetResult(out item); item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); DirectoryPath = path; return(true); } catch (Exception) { return(null); } finally { Marshal.ReleaseComObject(dialog); } }
public DialogResult ShowDialog(IWin32Window owner) { IShellItem result = null; IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); if (!string.IsNullOrEmpty(SelectedPath)) { SelectInitialPath(dialog, SelectedPath); } else if (!string.IsNullOrEmpty(SelectedDesktopAbsoluteParsing)) { SelectInitialPath(dialog, SelectedDesktopAbsoluteParsing); } if (!string.IsNullOrWhiteSpace(Title)) { dialog.SetTitle(Title); } dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_ALLNONSTORAGEITEMS); uint hr = dialog.Show(owner != null ? owner.Handle : IntPtr.Zero); if (hr == ERROR_CANCELLED) { return(DialogResult.Cancel); } if (hr != 0) { return(DialogResult.Abort); } dialog.GetResult(out result); string path; result.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); SelectedPath = path; result.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, out path); SelectedNormalDisplay = path; result.GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, out path); SelectedDesktopAbsoluteParsing = path; result.GetDisplayName(SIGDN.SIGDN_URL, out path); SelectedUrl = path; return(DialogResult.OK); }
private static String[] ShowDialogInner(IFileOpenDialog dialog, IntPtr parentHWnd, String title, String initialDirectory, String defaultFileName, IReadOnlyCollection <Filter> filters, Int32 selectedFilterZeroBasedIndex, FileOpenOptions flags) { flags = flags | FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (defaultFileName != null) { dialog.SetFileName(defaultFileName); } Utility.SetFilters(dialog, filters, selectedFilterZeroBasedIndex); HResult result = dialog.Show(parentHWnd); HResult cancelledAsHResult = Utility.HResultFromWin32((int)HResult.Win32ErrorCanceled); if (result == cancelledAsHResult) { // Cancelled return(null); } else { // OK IShellItemArray resultsArray; dialog.GetResults(out resultsArray); String[] fileNames = Utility.GetFileNames(resultsArray); return(fileNames); } }
private bool ShowDialog(IntPtr hwndOwner) { IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; if (!string.IsNullOrEmpty(InitialDirectory)) { var dir1 = InitialDirectory; dir1 = Environment.ExpandEnvironmentVariables(dir1); // expand environment variables dir1 = Path.GetFullPath(dir1); // resolve relative path IntPtr idl; uint atts = 0; if (SHILCreateFromPath(dir1, out idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } } } dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); dialog.SetTitle(Title); uint hr = dialog.Show(hwndOwner); if (hr == ERROR_CANCELLED) { _folderName = string.Empty; return(false); } if (hr != 0) { _folderName = string.Empty; return(false); } dialog.GetResult(out item); string path; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); _folderName = path; return(true); } finally { Marshal.ReleaseComObject(dialog); } }
private static String ShowDialogInner(IFileOpenDialog dialog, IntPtr parentWindowHandle, String title, String initialDirectory) { //IFileDialog ifd = dialog; FileOpenOptions flags = FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.PickFolders | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } HResult result = dialog.Show(parentWindowHandle); if (result == HResult.Ok) { IShellItemArray resultsArray; dialog.GetResults(out resultsArray); String[] fileNames = Utility.GetFileNames(resultsArray); return(fileNames.Length == 0 ? null : fileNames[0]); } else if (result == HResult_Win32_Canceled) { // Cancelled by user. return(null); } else { UInt32 win32ErrorCode = Utility.Win32ErrorFromHResult((UInt32)result); throw new Win32Exception(error: (Int32)win32ErrorCode); } }
/// <summary> /// 向用户显示 FolderBrowser 的对话框 /// </summary> /// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param> /// <returns></returns> public DialogResult ShowDialog(IWin32Window owner = null) { IntPtr hwndOwner = owner != null ? owner.Handle : GetActiveWindow(); IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; if (!string.IsNullOrEmpty(SelectedPath)) { IntPtr idl; uint atts = 0; if (SHILCreateFromPath(SelectedPath, out idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } } } dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); if (!string.IsNullOrEmpty(Description)) { dialog.SetTitle(Description); } uint hr = dialog.Show(hwndOwner); if (hr == ERROR_CANCELLED) { return(DialogResult.Cancel); } if (hr != 0) { return(DialogResult.Abort); } dialog.GetResult(out item); string path; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); SelectedPath = path; return(DialogResult.OK); } finally { Marshal.ReleaseComObject(dialog); } }
private static String ShowDialogInner(IFileOpenDialog dialog, IntPtr parentHWnd, String title, String initialDirectory) { //IFileDialog ifd = dialog; FileOpenOptions flags = FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.PickFolders | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } HResult result = dialog.Show(parentHWnd); HResult cancelledAsHResult = Utility.HResultFromWin32((int)HResult.Win32ErrorCanceled); if (result == cancelledAsHResult) { // Cancelled return(null); } else { // OK IShellItemArray resultsArray; dialog.GetResults(out resultsArray); String[] fileNames = Utility.GetFileNames(resultsArray); return(fileNames.Length == 0 ? null : fileNames[0]); } }
private void SetDialogProperties(IFileOpenDialog dialog) { dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); if (!string.IsNullOrEmpty(SelectedPath)) { string parent = Path.GetDirectoryName(SelectedPath); if (parent == null || !Directory.Exists(parent)) { dialog.SetFileName(SelectedPath); } else { string folder = Path.GetFileName(SelectedPath); dialog.SetFolder(CreateItemFromParsingName(parent)); dialog.SetFileName(folder); } } }
private void PickItem(object sender, EventArgs e) { var pfd = new IFileOpenDialog(); if (pfd != null) { var dwOptions = pfd.GetOptions(); pfd.SetOptions(dwOptions | FILEOPENDIALOGOPTIONS.FOS_ALLNONSTORAGEITEMS | FILEOPENDIALOGOPTIONS.FOS_PICKFOLDERS); pfd.SetTitle("Item Picker"); var hr = pfd.Show(Handle); if (hr.Succeeded) { if (pfd.GetResult() is IShellItem2 psi) { psiDrop = psi; StartWatching(); } } } }
private void AddButton_Click(object sender, EventArgs e) { var fbd = new IFileOpenDialog(); fbd.SetOptions(FOS.PICKFOLDERS | FOS.FORCEFILESYSTEM | FOS.ALLOWMULTISELECT | FOS.PATHMUSTEXIST | FOS.FILEMUSTEXIST); var hr = fbd.Show(Handle); if (hr >= 0) { fbd.GetResults(out var results); results.GetCount(out var count); for (var i = 0U; i < count; i++) { results.GetItemAt(i, out var si); si.GetDisplayName(SIGDN.FILESYSPATH, out var fullPath); AddDirectory(new DirectoryInfo(fullPath)); } } }
/// <summary> /// Shows the dialog. /// </summary> /// <returns>DialogResult.</returns> public DialogResult ShowDialog() { IntPtr hwndOwner = FindWindow("MsiDialogCloseClass", 0); IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; if (!string.IsNullOrEmpty(DirectoryPath)) { IntPtr idl; uint atts = 0; if (SHILCreateFromPath(DirectoryPath, out idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } } } dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); uint hr = dialog.Show(hwndOwner); if (hr == ERROR_CANCELLED) { return(DialogResult.Cancel); } if (hr != 0) { return(DialogResult.Abort); } dialog.GetResult(out item); string path; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); DirectoryPath = path; return(DialogResult.OK); } finally { Marshal.ReleaseComObject(dialog); } }
/// <summary> /// 向用户显示 FolderBrowser 的对话框 /// </summary> /// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param> /// <returns></returns> public bool?ShowDialog(Window owner = null) { ; IntPtr hwndOwner = owner != null ? new WindowInteropHelper(owner).Handle : Process.GetCurrentProcess().MainWindowHandle; IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; if (!string.IsNullOrEmpty(DirectoryPath)) { uint atts = 0; if (SHILCreateFromPath(DirectoryPath, out IntPtr idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } } } dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); uint hr = dialog.Show(hwndOwner); if (hr == ERROR_CANCELLED) { return(false); } if (hr != 0) { return(null); } dialog.GetResult(out item); item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out string path); DirectoryPath = path; return(true); } finally { Marshal.ReleaseComObject(dialog); } }
/// <summary> /// Runs a common dialog box with the specified owner. /// </summary> /// <param name="hwndOwner">Any object that implements IWin32Window that represents the top-level window that will own the modal dialog box.</param> /// <returns><see cref="DialogResult.OK"/> if the user clicks OK in the dialog box; otherwise, <see cref="DialogResult.Cancel"/>.</returns> public DialogResult ShowDialog(IntPtr hwndOwner) { IFileOpenDialog dialog = (IFileOpenDialog) new FileOpenDialog(); try { IShellItem item; if (!string.IsNullOrEmpty(SelectedPath)) { uint atts = 0; if (SHILCreateFromPath(SelectedPath, out IntPtr idl, ref atts) == 0) { if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } Marshal.FreeCoTaskMem(idl); } } dialog.SetOptions(0x20 | 0x40); // FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM uint hresult = dialog.Show(hwndOwner); if (hresult == 0x800704C7) // ERROR_CANCELLED { return(DialogResult.Cancel); } if (hresult != 0) { return(DialogResult.Abort); } dialog.GetResult(out item); item.GetDisplayName(0x80058000, out string path); // SIGDN_FILESYSPATH SelectedPath = path; return(DialogResult.OK); } finally { Marshal.ReleaseComObject(dialog); } }
public OpenFolderDialog() { _dialog = new IFileOpenDialog(); _dialog.SetOptions(FOS.PICKFOLDERS | FOS.FORCEFILESYSTEM | FOS.PATHMUSTEXIST | FOS.FILEMUSTEXIST); }
public SelectFolderDialog() { fod = new FileOpenDialog(); ifod = (IFileOpenDialog)fod; FOS fos; ifod.GetOptions(out fos); ifod.SetOptions(fos | FOS.FOS_PICKFOLDERS); // Fix an apparent bug where the default folder is inside the last selected folder rather than selecting it in parent folder IShellItem defaultFolder; ifod.GetFolder(out defaultFolder); if (defaultFolder != null) { IShellItem parentFolder; defaultFolder.GetParent(out parentFolder); if (parentFolder != null) { ifod.SetFolder(parentFolder); } } }
protected override void PerformAction(object parameter) { var album = parameter as FacebookPhotoAlbum; if (album == null) { return; } string folderPath = null; if (Utility.IsOSVistaOrNewer) { IFileOpenDialog pFolderDialog = null; try { pFolderDialog = CLSID.CoCreateInstance <IFileOpenDialog>(CLSID.FileOpenDialog); pFolderDialog.SetOptions(pFolderDialog.GetOptions() | FOS.NOREADONLYRETURN | FOS.PICKFOLDERS); pFolderDialog.SetTitle(string.Format("Select where to save \"{0}\"", album.Title)); pFolderDialog.SetOkButtonLabel("Save Album"); HRESULT hr = pFolderDialog.Show(new WindowInteropHelper(Application.Current.MainWindow).Handle); if (hr.Failed) { return; } IShellItem pItem = null; try { pItem = pFolderDialog.GetResult(); folderPath = ShellUtil.GetPathFromShellItem(pItem); } finally { Utility.SafeRelease(ref pItem); } } finally { Utility.SafeRelease(ref pFolderDialog); } } else { var folderDialog = new System.Windows.Forms.FolderBrowserDialog { Description = "Choose where to save the album.", ShowNewFolderButton = true, }; if (folderDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } folderPath = folderDialog.SelectedPath; } album.SaveToFolder(folderPath, _OnAlbumSaveProgressCallback, null); Process.Start(new ProcessStartInfo { FileName = folderPath }); }
void SetOptions(IFileOpenDialog dialog) { dialog.SetOptions(GetDialogOptions()); }
protected override bool RunDialog(IntPtr hwndOwner) { IntPtr pDisplayName = IntPtr.Zero; IFileOpenDialog dlg = null; IShellItem shStartupDir = null; try { dlg = (IFileOpenDialog)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7"))); FOS fos; dlg.GetOptions(out fos); fos = fos | FOS.FOS_FORCEFILESYSTEM | FOS.FOS_PICKFOLDERS | FOS.FOS_NOCHANGEDIR; dlg.SetOptions(fos); dlg.SetTitle(this.Description); // What if selectedPath is empty? if (this.SelectedPath != string.Empty) { var startupDir = System.IO.Path.GetFullPath(this.SelectedPath); if (System.IO.Directory.Exists(startupDir)) { Shell32.SHCreateItemFromParsingName(startupDir, IntPtr.Zero, new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe"), out shStartupDir); dlg.SetFolder(shStartupDir); } } // FIXME: should sig be Int32 (HRESULT)? Int32 hrResult = dlg.Show(hwndOwner); if (hrResult == -2147023673 /* HRESULT_FROM_WIN32(ERROR_CANCELLED) */) { return(false); } else if (hrResult < 0) { throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(hrResult); } IShellItem result; dlg.GetResult(out result); result.GetDisplayName(SIGDN.FILESYSPATH, out pDisplayName); this.SelectedPath = System.Runtime.InteropServices.Marshal.PtrToStringUni(pDisplayName); } finally { if (pDisplayName != IntPtr.Zero) { System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pDisplayName); } if (dlg != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dlg); } if (shStartupDir != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shStartupDir); } } // TODO: check if selected path exists? return(true); }
private static IFileOpenDialog CreateDialog() { Contract.Ensures( Contract.Result<IFileOpenDialog>() != null ); var options = FileDialogOptions.PickFolders | FileDialogOptions.ForceFileSystem | FileDialogOptions.PathMustExist; var dialog = new IFileOpenDialog(); dialog.SetOptions( options ); return dialog; }