Esempio n. 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);
            }
        }
        /// <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);
            }
        }
Esempio n. 4
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));
            }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
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);
            }
        }
Esempio n. 9
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);
            }
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
0
        /// <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 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]);
            }
        }
Esempio n. 13
0
        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();
                    }
                }
            }
        }
Esempio n. 14
0
        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
            });
        }
Esempio n. 15
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);
        }