private void SetDialogProperties(Interop.IFileDialog dialog)
        {
            // Description
            if (!string.IsNullOrEmpty(_description))
            {
                if (_useDescriptionForTitle)
                {
                    dialog.SetTitle(_description);
                }
                else
                {
                    Interop.IFileDialogCustomize customize = (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);
                }
            }
        }
Пример #2
0
 private bool RunFileDialog(IntPtr hwndOwner)
 {
     _hwndOwner = hwndOwner;
     Interop.IFileDialog dialog = null;
     try
     {
         dialog = CreateFileDialog();
         SetDialogProperties(dialog);
         int result = dialog.Show(hwndOwner);
         if (result < 0)
         {
             if (result == (int)HRESULT.ERROR_CANCELLED)
             {
                 return(false);
             }
             else
             {
                 throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(result);
             }
         }
         return(true);
     }
     finally
     {
         _hwndOwner = IntPtr.Zero;
         if (dialog != null)
         {
             System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dialog);
         }
     }
 }
        /// <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 WindowHandleWrapper(hwndOwner)) == DialogResult.OK);
            }

            Interop.IFileDialog dialog = null;
            try
            {
                dialog = new Interop.NativeFileOpenDialog();
                SetDialogProperties(dialog);
                int result = dialog.Show(hwndOwner);
                if (result < 0)
                {
                    if ((uint)result == (uint)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 CheckSelectedFolder(Interop.IFileDialog pfd, Interop.IShellItem psiFolder)
        {
            //
            NativeMethods.SFGAOF fileSystemFlags = 0;
            psiFolder.GetAttributes(NativeMethods.SFGAOF.SFGAO_FILESYSTEM, out fileSystemFlags);
            if (fileSystemFlags != 0)
            {
                // A filesystem folder, but is it valid?
                string path;
                psiFolder.GetDisplayName(NativeMethods.SIGDN.SIGDN_FILESYSPATH, out path);

                if (IsShortcut(path))
                {
                    path = ResolveShortcut(path);
                }

                //Console.WriteLine("filesyspath path: {0}", path);
                _dialog.DoFolderSelected(path);
            }
            else
            {
                // Not a filesystem, let the user know
                _dialog.DoFolderSelected("");
            }
        }
 private void GetResult(Interop.IFileDialog dialog)
 {
     Interop.IShellItem item;
     dialog.GetResult(out item);
     item.GetDisplayName(NativeMethods.SIGDN.SIGDN_FILESYSPATH, out _selectedPath);
 }
 private void CheckSelectedFolder(Interop.IFileDialog pfd)
 {
     Interop.IShellItem psiFolder;
     pfd.GetCurrentSelection(out psiFolder);
     CheckSelectedFolder(pfd, psiFolder);
 }
 public void OnFolderChange(Interop.IFileDialog pfd)
 {
     //CheckSelectedFolder(pfd);
 }
 public void OnSelectionChange(Interop.IFileDialog pfd)
 {
     CheckSelectedFolder(pfd);
 }
 public Interop.HRESULT OnFolderChanging(Interop.IFileDialog pfd, Interop.IShellItem psiFolder)
 {
     //CheckSelectedFolder(psiFolder);
     return(Interop.HRESULT.S_OK);
 }
 public Interop.HRESULT OnFileOk(Interop.IFileDialog pfd)
 {
     // [seanba] - We could put extra validation here to stop dialog from closing.
     return(Interop.HRESULT.S_OK);
 }
 public void OnOverwrite(Interop.IFileDialog pfd, Interop.IShellItem psi, out NativeMethods.FDE_OVERWRITE_RESPONSE pResponse)
 {
     pResponse = NativeMethods.FDE_OVERWRITE_RESPONSE.FDEOR_DEFAULT;
 }
 public void OnTypeChange(Interop.IFileDialog pfd)
 {
 }
 public void OnShareViolation(Interop.IFileDialog pfd, Interop.IShellItem psi, out NativeMethods.FDE_SHAREVIOLATION_RESPONSE pResponse)
 {
     pResponse = NativeMethods.FDE_SHAREVIOLATION_RESPONSE.FDESVR_DEFAULT;
 }