コード例 #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);
            }
        }
コード例 #2
0
        /// <summary>
        /// 向用户显示 FolderBrowser 的对话框
        /// </summary>
        /// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param>
        /// <returns></returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            IFileOpenDialog dialog = null;

            try
            {
                dialog = (IFileOpenDialog) new FileOpenDialogRCW();
                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);
                }
            }
        }
コード例 #3
0
        private void SetDialogResults(IFileOpenDialog dialog)
        {
            IShellItem item;

            if (!Multiselect)
            {
                dialog.GetResult(out item);
                GetPathAndElementName(item, out string path, out string value);
                this.SelectedPath         = path;
                this.SelectedPaths        = new[] { path };
                this.SelectedElementName  = value;
                this.SelectedElementNames = new[] { value };
            }
            else
            {
                dialog.GetResults(out IShellItemArray items);

                items.GetCount(out uint count);

                this.SelectedPaths        = new string[count];
                this.SelectedElementNames = new string[count];

                for (uint i = 0; i < count; ++i)
                {
                    items.GetItemAt(i, out item);
                    GetPathAndElementName(item, out string path, out string value);
                    this.SelectedPaths[i]        = path;
                    this.SelectedElementNames[i] = value;
                }

                this.SelectedPath        = null;
                this.SelectedElementName = null;
            }
        }
コード例 #4
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);
コード例 #5
0
        /// <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);
            }
        }
コード例 #6
0
        private void SetOptions([NotNull] IFileOpenDialog dialog)
        {
            if (dialog == null)
            {
                throw new ArgumentNullException(nameof(dialog));
            }

            dialog.SetOptions(GetDialogOptions());
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        private void GetResult(IFileOpenDialog dialog)
        {
            IShellItem item;

            dialog.GetResult(out item);
            string path;

            item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path);
            SelectedPath = path;
        }
コード例 #9
0
ファイル: FolderBrowser2.cs プロジェクト: v6ak/DemoCleaner3
        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));
            }
        }
コード例 #10
0
 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);
         }
     }
 }
コード例 #11
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);
            }
        }
コード例 #12
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);
            }
        }
コード例 #13
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);
            }
        }
コード例 #14
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);
        }
    }
コード例 #15
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);
            }
        }
コード例 #16
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);
        }
コード例 #17
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);
            }
        }
コード例 #18
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);
            }
        }
コード例 #19
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);
            }
        }
コード例 #20
0
        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]);
            }
        }
コード例 #21
0
        private bool ShowDialog(IntPtr owner)
        {
            IFileOpenDialog fod = null;

            try
            {
                fod = new FileOpenDialog() as IFileOpenDialog;
                fod.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM);

                if (!string.IsNullOrEmpty(InitialPath))
                {
                    uint attribute = 0U;
                    if ((SHILCreateFromPath(InitialPath, out IntPtr idl, ref attribute) == S_OK) &&
                        (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out IShellItem item) == S_OK))
                    {
                        fod.SetFolder(item);
                    }
                }

                if (!string.IsNullOrEmpty(Title))
                {
                    fod.SetTitle(Title);
                }

                if (fod.Show(owner) != S_OK)
                {
                    return(false);
                }

                SelectedPath = fod.GetResult().GetDisplayName(SIGDN.SIGDN_FILESYSPATH);
                return(true);
            }
            catch (COMException ce)
            {
                Debug.WriteLine($"Failed to manage open folder dialog.\r\n{ce}");
                return(false);
            }
            finally
            {
                if (fod is not null)
                {
                    Marshal.FinalReleaseComObject(fod);
                }
            }
        }
コード例 #22
0
        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);
            }
        }
コード例 #23
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);
                }
            }
        }
コード例 #24
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();
                    }
                }
            }
        }
コード例 #25
0
        private void AddButton_Click(object sender, EventArgs e)
        {
            var fbd = new IFileOpenDialog();

            fbd.SetOptions(FOS.PICKFOLDERS | FOS.FORCEFILESYSTEM | FOS.ALLOWMULTISELECT | FOS.PATHMUSTEXIST | FOS.FILEMUSTEXIST);
            var hr = fbd.Show(Handle);

            if (hr >= 0)
            {
                fbd.GetResults(out var results);
                results.GetCount(out var count);

                for (var i = 0U; i < count; i++)
                {
                    results.GetItemAt(i, out var si);
                    si.GetDisplayName(SIGDN.FILESYSPATH, out var fullPath);
                    AddDirectory(new DirectoryInfo(fullPath));
                }
            }
        }
コード例 #26
0
        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);
            }
        }
コード例 #27
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);
            }
        }
コード例 #28
0
        private static void DoGetOpenFileDialogFilename(NktRemoteBridge remoteBridge, Process proc)
        {
            IFileOpenDialog dlg = null;
            object obj = null;
            IntPtr hWnd;
            string s;

            try
            {
                hWnd = remoteBridge.FindWindow(proc.Id, IntPtr.Zero, "Open", "#32770", false);
                if (hWnd == IntPtr.Zero)
                    hWnd = remoteBridge.FindWindow(proc.Id, IntPtr.Zero, "Abrir", "#32770", false);
                if (hWnd == IntPtr.Zero)
                {
                    Console.WriteLine("Cannot find OpenFileDialog window");
                    return;
                }
                obj = remoteBridge.GetComInterfaceFromHwnd(proc.Id, hWnd, ShellIIDGuid.IFileDialog);
                if (obj == null)
                {
                    Console.WriteLine("Cannot find retrieve IFileOpenDialog interface");
                    return;
                }
                dlg = obj as IFileOpenDialog;
                dlg.GetFileName(out s);
                Console.WriteLine("Filename: [" + s + "]");
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                //if (dlg != null)
                //    Marshal.ReleaseComObject(dlg);
                if (obj != null)
                    Marshal.ReleaseComObject(obj);
            }
            return;
        }
コード例 #29
0
        /// <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);
            }
        }
コード例 #30
0
        private void SetDialogResults([NotNull] IFileOpenDialog dialog)
        {
            if (dialog == null)
            {
                throw new ArgumentNullException(nameof(dialog));
            }

            IShellItem item;

            try
            {
                dialog.GetResult(out item);
                GetPathAndElementName(item, out var path, out var value);
                SelectedPath         = path;
                SelectedPaths        = new[] { path };
                SelectedElementName  = value;
                SelectedElementNames = new[] { value };
            }
            catch (COMException ex) when(ex.HResult == -2147418113)
            {
                dialog.GetResults(out var items);

                items.GetCount(out var count);

                SelectedPaths        = new string[count];
                SelectedElementNames = new string[count];

                for (uint i = 0; i < count; ++i)
                {
                    items.GetItemAt(i, out item);
                    GetPathAndElementName(item, out var path, out var value);
                    SelectedPaths[i]        = path;
                    SelectedElementNames[i] = value;
                }

                SelectedPath        = null;
                SelectedElementName = null;
            }
        }
コード例 #31
0
		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);
				}
			}
		}
コード例 #32
0
		public SelectFileDialog()
		{
			fod = new FileOpenDialog();
			ifod = (IFileOpenDialog)fod;
		}
コード例 #33
0
        private static string GetSelectedPath( IFileOpenDialog fileDialog )
        {
            string path;
            IShellItem item;

            fileDialog.GetResult( out item );
            item.GetDisplayName( ShellItemDisplayName.FileSystemPath, out path );
            Marshal.FinalReleaseComObject( item );

            return path;
        }
コード例 #34
0
        private static IFileOpenDialog CreateDialog()
        {
            Contract.Ensures( Contract.Result<IFileOpenDialog>() != null );

            var options = FileDialogOptions.PickFolders | FileDialogOptions.ForceFileSystem | FileDialogOptions.PathMustExist;
            var dialog = new IFileOpenDialog();
            dialog.SetOptions( options );

            return dialog;
        }