private void SetDialogProperties(API.IFileDialog dialog) { // Description if (!string.IsNullOrEmpty(_description)) { if (_useDescriptionForTitle) { dialog.SetTitle(_description); } else { API.IFileDialogCustomize customize = (API.IFileDialogCustomize)dialog; customize.AddText(0, _description); } } dialog.SetOptions(API.FOS.FOS_PICKFOLDERS | API.FOS.FOS_FORCEFILESYSTEM | API.FOS.FOS_FILEMUSTEXIST); 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(API.CreateItemFromParsingName(parent)); dialog.SetFileName(folder); } } }
/// <summary> /// Specifies a common dialog box. /// </summary> /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param> /// <returns><see langword="true" /> if the file could be opened; otherwise, <see langword="false" />.</returns> protected override bool RunDialog(IntPtr hwndOwner) { if (_downlevelDialog != null) { return(_downlevelDialog.ShowDialog(hwndOwner == IntPtr.Zero ? null : new API.WindowHandleWrapper(hwndOwner)) == DialogResult.OK); } API.IFileDialog dialog = null; try { dialog = new API.NativeFileOpenDialog(); SetDialogProperties(dialog); int result = dialog.Show(hwndOwner); if (result < 0) { if ((uint)result == (uint)API.HRESULT.ERROR_CANCELLED) { return(false); } else { throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(result); } } GetResult(dialog); return(true); } finally { if (dialog != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dialog); } } }
private void GetResult(API.IFileDialog dialog) { API.IShellItem item; dialog.GetResult(out item); item.GetDisplayName(API.SIGDN.SIGDN_FILESYSPATH, out _selectedPath); }