private void SetDialogResults(IFileOpenDialog dialog) { IShellItem item; if (!Multiselect) { dialog.GetResult(out item); GetPathAndElementName(item, out string path, out string value); this.SelectedPath = path; this.SelectedPaths = new[] { path }; this.SelectedElementName = value; this.SelectedElementNames = new[] { value }; } else { dialog.GetResults(out IShellItemArray items); items.GetCount(out uint count); this.SelectedPaths = new string[count]; this.SelectedElementNames = new string[count]; for (uint i = 0; i < count; ++i) { items.GetItemAt(i, out item); GetPathAndElementName(item, out string path, out string value); this.SelectedPaths[i] = path; this.SelectedElementNames[i] = value; } this.SelectedPath = null; this.SelectedElementName = null; } }
/// <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 GetResult(IFileOpenDialog dialog) { IShellItem item; dialog.GetResult(out item); string path; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); SelectedPath = path; }
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)); } }
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); }
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); } }
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); } }
/// <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 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(); } } } }
/// <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); } }
private void SetDialogResults([NotNull] IFileOpenDialog dialog) { if (dialog == null) { throw new ArgumentNullException(nameof(dialog)); } IShellItem item; try { dialog.GetResult(out item); GetPathAndElementName(item, out var path, out var value); SelectedPath = path; SelectedPaths = new[] { path }; SelectedElementName = value; SelectedElementNames = new[] { value }; } catch (COMException ex) when(ex.HResult == -2147418113) { dialog.GetResults(out var items); items.GetCount(out var count); SelectedPaths = new string[count]; SelectedElementNames = new string[count]; for (uint i = 0; i < count; ++i) { items.GetItemAt(i, out item); GetPathAndElementName(item, out var path, out var value); SelectedPaths[i] = path; SelectedElementNames[i] = value; } SelectedPath = null; SelectedElementName = null; } }
internal override string[] ProcessVistaFiles(IFileDialog dialog) { IFileOpenDialog fileOpenDialog = (IFileOpenDialog)dialog; if (this.Multiselect) { IShellItemArray results = fileOpenDialog.GetResults(); uint count = results.GetCount(); string[] array = new string[count]; for (uint num = 0U; num < count; num += 1U) { IShellItem itemAt = results.GetItemAt(num); array[(int)num] = itemAt.GetDisplayName((SIGDN)2147647488U); } return(array); } IShellItem result = fileOpenDialog.GetResult(); return(new string[] { result.GetDisplayName((SIGDN)2147647488U) }); }
/// <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); } }
private static string GetSelectedPath( IFileOpenDialog fileDialog ) { string path; IShellItem item; fileDialog.GetResult( out item ); item.GetDisplayName( ShellItemDisplayName.FileSystemPath, out path ); Marshal.FinalReleaseComObject( item ); return path; }
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 }); }
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); }