private void SetDialogProperties(Ookii.Dialogs.Interop.IFileDialog dialog)
        {
            // Description
            if (!string.IsNullOrEmpty(_description))
            {
                if (_useDescriptionForTitle)
                {
                    dialog.SetTitle(_description);
                }
                else
                {
                    Ookii.Dialogs.Interop.IFileDialogCustomize customize = (Ookii.Dialogs.Interop.IFileDialogCustomize)dialog;
                    customize.AddText(0, _description);
                }
            }

            dialog.SetOptions(NativeMethods.FOS.FOS_PICKFOLDERS | NativeMethods.FOS.FOS_FORCEFILESYSTEM | NativeMethods.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(NativeMethods.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 Interop.WindowHandleWrapper(hwndOwner)) == DialogResult.OK);
            }

            Ookii.Dialogs.Interop.IFileDialog dialog = null;
            try
            {
                dialog = new Ookii.Dialogs.Interop.NativeFileOpenDialog();
                SetDialogProperties(dialog);
                int result = dialog.Show(hwndOwner);
                if (result < 0)
                {
                    if ((uint)result == (uint)Interop.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);
                }
            }
        }
 public Interop.HRESULT OnFileOk(Ookii.Dialogs.Interop.IFileDialog pfd)
 {
     if (_dialog.DoFileOk(pfd))
     {
         return(Ookii.Dialogs.Interop.HRESULT.S_OK);
     }
     else
     {
         return(Ookii.Dialogs.Interop.HRESULT.S_FALSE);
     }
 }
 public void OnOverwrite(Ookii.Dialogs.Interop.IFileDialog pfd, Ookii.Dialogs.Interop.IShellItem psi, out NativeMethods.FDE_OVERWRITE_RESPONSE pResponse)
 {
     pResponse = NativeMethods.FDE_OVERWRITE_RESPONSE.FDEOR_DEFAULT;
 }
 public void OnTypeChange(Ookii.Dialogs.Interop.IFileDialog pfd)
 {
 }
 public void OnShareViolation(Ookii.Dialogs.Interop.IFileDialog pfd, Ookii.Dialogs.Interop.IShellItem psi, out NativeMethods.FDE_SHAREVIOLATION_RESPONSE pResponse)
 {
     pResponse = NativeMethods.FDE_SHAREVIOLATION_RESPONSE.FDESVR_DEFAULT;
 }
 public void OnSelectionChange(Ookii.Dialogs.Interop.IFileDialog pfd)
 {
 }
 public void OnFolderChange(Ookii.Dialogs.Interop.IFileDialog pfd)
 {
 }
 public Interop.HRESULT OnFolderChanging(Ookii.Dialogs.Interop.IFileDialog pfd, Ookii.Dialogs.Interop.IShellItem psiFolder)
 {
     return(Ookii.Dialogs.Interop.HRESULT.S_OK);
 }
 private void GetResult(Ookii.Dialogs.Interop.IFileDialog dialog)
 {
     Ookii.Dialogs.Interop.IShellItem item;
     dialog.GetResult(out item);
     item.GetDisplayName(NativeMethods.SIGDN.SIGDN_FILESYSPATH, out _selectedPath);
 }