public static string Show(IWin32Window owner, string title, BrowseForFolderOptions options)
        {
            NativeMethods.BROWSEINFO mbif = new NativeMethods.BROWSEINFO();

            mbif.hwndOwner = owner == null ? IntPtr.Zero : owner.Handle;
            mbif.lpfn      = IntPtr.Zero;
            mbif.lpszTitle = title;
            mbif.ulFlags   = (uint)options;

            int itemlstptr = 0;

            itemlstptr = NativeMethods.SHBrowseForFolder(mbif);

            IntPtr sourcePath = Marshal.AllocCoTaskMem(256);

            bool bRes = false;

            string dirPath = null;

            if (itemlstptr >= 0)
            {
                bRes = NativeMethods.SHGetPathFromIDList(itemlstptr, sourcePath);

                dirPath = Marshal.PtrToStringAnsi(sourcePath);
            }

            Marshal.FreeCoTaskMem(sourcePath);

            if (dirPath != null && dirPath.Trim() == "")
            {
                dirPath = null;
            }

            return(dirPath);
        }
示例#2
0
        private async Task<StorageFolder> PickSingleFolderAsyncImpl()
        {
            NativeMethods.BROWSEINFO pbi = new NativeMethods.BROWSEINFO();
            pbi.ulFlags = 0x41;
            pbi.pszDisplayName = new string('\0', 256);

            IntPtr pidl = NativeMethods.SHBrowseForFolder(ref pbi);
            if(pidl != IntPtr.Zero)
            {
                IntPtr pathp = Marshal.AllocHGlobal(256);
                bool success = NativeMethods.SHGetPathFromIDList(pidl, pathp);
                Marshal.FreeCoTaskMem(pidl);

                if (success)
                {
                    string path = Marshal.PtrToStringAnsi(pathp);
                    Marshal.FreeHGlobal(pathp);
                    return new StorageFolder(path);
                }
            }

            return null;
        }
示例#3
0
        private async Task <StorageFolder> DoPickSingleFolderAsync()
        {
            NativeMethods.BROWSEINFO pbi = new NativeMethods.BROWSEINFO();
            pbi.ulFlags        = 0x41;
            pbi.pszDisplayName = new string('\0', 256);

            IntPtr pidl = NativeMethods.SHBrowseForFolder(ref pbi);

            if (pidl != IntPtr.Zero)
            {
                IntPtr pathp   = Marshal.AllocHGlobal(256);
                bool   success = NativeMethods.SHGetPathFromIDList(pidl, pathp);
                Marshal.FreeCoTaskMem(pidl);

                if (success)
                {
                    string path = Marshal.PtrToStringAnsi(pathp);
                    Marshal.FreeHGlobal(pathp);
                    return(new StorageFolder(path));
                }
            }

            return(null);
        }
示例#4
0
        private DialogResult ShowNativeDialog(IWin32Window owner)
        {
            var rootFolder = IntPtr.Zero;

            NativeMethods.SHGetSpecialFolderLocation(owner.Handle, (int)RootFolder, ref rootFolder);

            if (rootFolder == IntPtr.Zero)
            {
                NativeMethods.SHGetSpecialFolderLocation(owner.Handle, (int)Environment.SpecialFolder.Desktop, ref rootFolder);

                if (rootFolder == IntPtr.Zero)
                {
                    throw new InvalidOperationException("Cannot resolve root folder");
                }
            }

            uint options = NativeMethods.BIF_NEWDIALOGSTYLE;

            if (ShowEditBox)
            {
                options |= NativeMethods.BIF_EDITBOX;
            }
            if (!ShowNewFolderButton)
            {
                options |= NativeMethods.BIF_NONEWFOLDERBUTTON;
            }

            var result       = IntPtr.Zero;
            var displayName  = IntPtr.Zero;
            var selectedPath = IntPtr.Zero;

            try
            {
                var bi = new NativeMethods.BROWSEINFO();

                displayName  = Marshal.AllocHGlobal(NativeMethods.MAX_PATH * Marshal.SystemDefaultCharSize);
                selectedPath = Marshal.AllocHGlobal(NativeMethods.MAX_PATH * Marshal.SystemDefaultCharSize);

                bi.pidlRoot       = rootFolder;
                bi.hwndOwner      = owner.Handle;
                bi.pszDisplayName = displayName;
                bi.lpszTitle      = Description;
                bi.ulFlags        = options;

                result = NativeMethods.SHBrowseForFolder(bi);

                if (result != IntPtr.Zero)
                {
                    NativeMethods.SHGetPathFromIDList(result, selectedPath);

                    SelectedPath = Marshal.PtrToStringAuto(selectedPath);

                    return(DialogResult.OK);
                }

                return(DialogResult.Cancel);
            }
            finally
            {
                NativeMethods.CoTaskMemFree(rootFolder);

                if (result != IntPtr.Zero)
                {
                    NativeMethods.CoTaskMemFree(result);
                }

                if (selectedPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(selectedPath);
                }

                if (displayName != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(displayName);
                }
            }
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool result = false;

            IntPtr pidlRoot     = IntPtr.Zero,
                   pszPath      = IntPtr.Zero,
                   pidlSelected = IntPtr.Zero;

            SelectedPath = string.Empty;

            try
            {
                if (RootType == RootType.SpecialFolder)
                {
                    NativeMethods.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, IntPtr.Zero, 0, out pidlRoot);
                }
                else // RootType == Path
                {
                    uint iAttribute;
                    NativeMethods.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute);
                }

                var browseInfo = new NativeMethods.BROWSEINFO
                {
                    HwndOwner   = hwndOwner,
                    Root        = pidlRoot,
                    DisplayName = new string(' ', 256),
                    Title       = Title,
                    Flags       = (uint)_dialogOptions,
                    LParam      = 0,
                    Callback    = HookProc
                };

                // Show dialog
                pidlSelected = NativeMethods.SHBrowseForFolder(ref browseInfo);

                if (pidlSelected != IntPtr.Zero)
                {
                    result = true;

                    pszPath = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                    NativeMethods.SHGetPathFromIDList(pidlSelected, pszPath);

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally // release all unmanaged resources
            {
                NativeMethods.IMalloc malloc = NativeMethods.GetSHMalloc();

                if (pidlRoot != IntPtr.Zero)
                {
                    malloc.Free(pidlRoot);
                }

                if (pidlSelected != IntPtr.Zero)
                {
                    malloc.Free(pidlSelected);
                }

                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }

                Marshal.ReleaseComObject(malloc);
            }

            return(result);
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool result = false;

            IntPtr pidlRoot = IntPtr.Zero,
                pszPath = IntPtr.Zero,
                pidlSelected = IntPtr.Zero;

            SelectedPath = string.Empty;

            try
            {
                if (RootType == RootType.SpecialFolder)
                {
                    NativeMethods.SHGetFolderLocation(hwndOwner, (int) RootSpecialFolder, IntPtr.Zero, 0, out pidlRoot);
                }
                else // RootType == Path
                {
                    uint iAttribute;
                    NativeMethods.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute);
                }

                var browseInfo = new NativeMethods.BROWSEINFO
                {
                    HwndOwner = hwndOwner,
                    Root = pidlRoot,
                    DisplayName = new string(' ', 256),
                    Title = Title,
                    Flags = (uint) _dialogOptions,
                    LParam = 0,
                    Callback = HookProc
                };

                // Show dialog
                pidlSelected = NativeMethods.SHBrowseForFolder(ref browseInfo);

                if (pidlSelected != IntPtr.Zero)
                {
                    result = true;

                    pszPath = Marshal.AllocHGlobal(260*Marshal.SystemDefaultCharSize);
                    NativeMethods.SHGetPathFromIDList(pidlSelected, pszPath);

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally // release all unmanaged resources
            {
                NativeMethods.IMalloc malloc = NativeMethods.GetSHMalloc();

                if (pidlRoot != IntPtr.Zero)
                {
                    malloc.Free(pidlRoot);
                }

                if (pidlSelected != IntPtr.Zero)
                {
                    malloc.Free(pidlSelected);
                }

                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }

                Marshal.ReleaseComObject(malloc);
            }

            return result;
        }
示例#7
0
        private DialogResult ShowNativeDialog(IWin32Window owner)
        {
            var rootFolder = IntPtr.Zero;

            NativeMethods.SHGetSpecialFolderLocation(owner.Handle, (int)RootFolder, ref rootFolder);

            if (rootFolder == IntPtr.Zero)
            {
                NativeMethods.SHGetSpecialFolderLocation(owner.Handle, (int)Environment.SpecialFolder.Desktop, ref rootFolder);

                if (rootFolder == IntPtr.Zero)
                    throw new InvalidOperationException("Cannot resolve root folder");
            }

            uint options = NativeMethods.BIF_NEWDIALOGSTYLE;
            if (ShowEditBox)
                options |= NativeMethods.BIF_EDITBOX;
            if (!ShowNewFolderButton)
                options |= NativeMethods.BIF_NONEWFOLDERBUTTON;

            var result = IntPtr.Zero;
            var displayName = IntPtr.Zero;
            var selectedPath = IntPtr.Zero;

            try
            {
                var bi = new NativeMethods.BROWSEINFO();

                displayName = Marshal.AllocHGlobal(NativeMethods.MAX_PATH * Marshal.SystemDefaultCharSize);
                selectedPath = Marshal.AllocHGlobal(NativeMethods.MAX_PATH * Marshal.SystemDefaultCharSize);

                bi.pidlRoot = rootFolder;
                bi.hwndOwner = owner.Handle;
                bi.pszDisplayName = displayName;
                bi.lpszTitle = Description;
                bi.ulFlags = options;

                result = NativeMethods.SHBrowseForFolder(bi);

                if (result != IntPtr.Zero)
                {
                    NativeMethods.SHGetPathFromIDList(result, selectedPath);

                    SelectedPath = Marshal.PtrToStringAuto(selectedPath);

                    return DialogResult.OK;
                }

                return DialogResult.Cancel;
            }
            finally
            {
                NativeMethods.CoTaskMemFree(rootFolder);

                if (result != IntPtr.Zero)
                    NativeMethods.CoTaskMemFree(result);

                if (selectedPath != IntPtr.Zero)
                    Marshal.FreeHGlobal(selectedPath);

                if (displayName != IntPtr.Zero)
                    Marshal.FreeHGlobal(displayName);
            }
        }