Exemple #1
0
        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);
            }
        }
Exemple #2
0
        // This opens up the common file dialog to an IShellItem and waits for the user to select a file from the results.
        // It then displays the name of the selected item in a message box.
        static void OpenCommonFileDialogTo(IShellItem pShellItemSearch)
        {
            // Create an instance of IFileOpenDialog
            IFileDialog pFileDialog = new IFileOpenDialog();

            // Set it to the folder we want to show
            pFileDialog.SetFolder(pShellItemSearch);
            pFileDialog.Show(default);
        /// <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 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);
            }
        }
Exemple #5
0
        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));
            }
        }
 void SetInitialFolder(IFileOpenDialog dialog)
 {
     if (!string.IsNullOrEmpty(SelectedPath))
     {
         uint atts = 0;
         if (NativeMethods.SHILCreateFromPath(SelectedPath, out IntPtr idl, ref atts) == 0 &&
             NativeMethods.SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out IShellItem item) == 0)
         {
             dialog.SetFolder(item);
         }
     }
 }
Exemple #7
0
        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);
            }
        }
Exemple #8
0
        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);
            }
        }
Exemple #9
0
        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);
            }
        }
Exemple #10
0
    private void SelectInitialPath(IFileOpenDialog dialog, string path)
    {
        uint   atts = 0;
        IntPtr idl  = IntPtr.Zero;

        if (SHILCreateFromPath(path, out idl, ref atts) == 0)
        {
            if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out IShellItem initial) == 0)
            {
                dialog.SetFolder(initial);
            }
            Marshal.FreeCoTaskMem(idl);
        }
    }
Exemple #11
0
        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);
            }
        }
Exemple #12
0
        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);
            }
        }
Exemple #14
0
        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);
            }
        }
        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]);
            }
        }
Exemple #16
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 SetFolder(IFileOpenDialog dialog, string path)
        {
            uint attributes = 0;

            if (NativeMethods.SHILCreateFromPath(path, out var idl, ref attributes) != 0)
            {
                return;
            }

            if (NativeMethods.SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out var item) == 0)
            {
                dialog.SetFolder(item);
            }

            if (idl != IntPtr.Zero)
            {
                Marshal.FreeCoTaskMem(idl);
            }
        }
        private void SetInitialFolder([NotNull] IFileOpenDialog dialog)
        {
            if (dialog == null)
            {
                throw new ArgumentNullException(nameof(dialog));
            }

            if (string.IsNullOrEmpty(SelectedPath))
            {
                return;
            }

            uint atts = 0;

            if (NativeMethods.SHILCreateFromPath(SelectedPath, out var idl, ref atts) == 0 &&
                NativeMethods.SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out var item) == 0)
            {
                dialog.SetFolder(item);
            }
        }
Exemple #19
0
        /// <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);
            }
        }
Exemple #22
0
        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);
        }
		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);
				}
			}
		}