public string SelectFolder(string caption, string initialPath, IntPtr parentHandle)
        {
            _initialPath = initialPath;
            StringBuilder sb            = new StringBuilder(256);
            IntPtr        bufferAddress = Marshal.AllocHGlobal(256);;
            IntPtr        pidl          = IntPtr.Zero;
            BROWSEINFO    bi            = new BROWSEINFO();

            bi.hwndOwner      = parentHandle;
            bi.pidlRoot       = IntPtr.Zero;
            bi.pszDisplayName = initialPath;
            bi.lpszTitle      = caption;
            bi.ulFlags        = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE;
            bi.lpfn           = new BrowseCallBackProc(OnBrowseEvent);
            bi.lParam         = IntPtr.Zero;
            bi.iImage         = 0;

            try
            {
                pidl = SHBrowseForFolder(ref bi);
                if (true != SHGetPathFromIDList(pidl, bufferAddress))
                {
                    return(null);
                }
                sb.Append(Marshal.PtrToStringAuto(bufferAddress));
            }
            finally
            {
                // Caller is responsible for freeing this memory.
                Marshal.FreeCoTaskMem(pidl);
            }

            return(sb.ToString());
        }
Beispiel #2
0
        public static void Show(string title, DeferredInvokeHandler callback, bool validate)
        {
            IntPtr winHandle = Application.Window.Handle;
            Thread thread    = new Thread(args =>
            {
                BROWSEINFO bi     = new BROWSEINFO();
                bi.hwndOwner      = winHandle;
                bi.pidlRoot       = IntPtr.Zero;
                bi.pszDisplayName = new string(' ', 261);
                if (!string.IsNullOrEmpty(title))
                {
                    bi.pszTitle = title;
                }
                bi.ulFlags = 112U;
                bi.lpfn    = new BFFCALLBACK(Validate);
                bi.lParam  = validate ? ValidateWritable : NoValidation;
                bi.iImage  = 0;
                IntPtr num = SHBrowseForFolder(ref bi);
                string str = null;
                if (num != IntPtr.Zero)
                {
                    StringBuilder Path = new StringBuilder(261);
                    if (SHGetPathFromIDList(num, Path) != 0)
                    {
                        str = Path.ToString();
                    }
                }
                Marshal.FreeCoTaskMem(num);
                Application.DeferredInvoke(callback, str);
            });

            thread.TrySetApartmentState(ApartmentState.STA);
            thread.Start();
        }
Beispiel #3
0
        protected override bool RunDialog(System.IntPtr hwndOwner)
        {
            string m_DisplayName = "";
            string sDisplay;
            IntPtr ptrRet;

            // Get IMalloc interface
            WindowsAPI.SHGetMalloc(out ptrRet);

            Type    mallocType = System.Type.GetType("IMalloc");
            IMalloc pMalloc    = ( IMalloc )Marshal.GetTypedObjectForIUnknown(ptrRet, mallocType);
            IntPtr  pidlRoot   = IntPtr.Zero;

            BROWSEINFO bi = new BROWSEINFO();

            bi.hwndOwner      = hwndOwner;
            bi.pidlRoot       = pidlRoot; //TODO: Root`s selection
            bi.pszDisplayName = new string( ' ', 256 );
            bi.lpszTitle      = Title;
            bi.ulFlags        = GetFlagsValue();
            bi.lParam         = 0;
            bi.lpfn           = new WindowsAPI.BrowseCallbackProc(this.HookProc);

            IntPtr pidlSelected;

            pidlSelected  = WindowsAPI.SHBrowseForFolder(ref bi);
            m_DisplayName = bi.pszDisplayName.ToString();

            // if display name is whitespace then return FAIL
            if (m_DisplayName.Trim() == string.Empty)
            {
                return(false);
            }

            IShellFolder isf = GetDesktopFolder();

            STRRET ptrDisplayName;

            isf.GetDisplayNameOf(pidlSelected,
                                 (uint)SHGNO.SHGDN_NORMAL | (uint)SHGNO.SHGDN_FORPARSING,
                                 out ptrDisplayName);

            WindowsAPI.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay);
            m_sFullName = sDisplay;

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

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

            Marshal.ReleaseComObject(isf);
            Marshal.ReleaseComObject(pMalloc);

            return(true);
        }
Beispiel #4
0
    protected bool RunDialog(IntPtr hWndOwner)
    {
        BROWSEINFO udtBI    = new BROWSEINFO();
        IntPtr     lpIDList = default(IntPtr);
        GCHandle   hTitle   = GCHandle.Alloc(Title, GCHandleType.Pinned);

        udtBI.hWndOwner = hWndOwner;
        udtBI.lpszTitle = Title;
        udtBI.ulFlags   = (int)BrowseFor;
        StringBuilder buffer = new StringBuilder(MAX_PATH);

        buffer.Length        = MAX_PATH;
        udtBI.pszDisplayName = buffer.ToString();
        lpIDList             = SHBrowseForFolder(ref udtBI);
        hTitle.Free();
        if (lpIDList.ToInt64() != 0)
        {
            if (BrowseFor == BrowseForTypes.Computers)
            {
                m_Selected = udtBI.pszDisplayName.Trim();
            }
            else
            {
                StringBuilder path = new StringBuilder(MAX_PATH);
                SHGetPathFromIDList(lpIDList, path);
                m_Selected = path.ToString();
            }
            CoTaskMemFree(lpIDList);
        }
        else
        {
            return(false);
        }
        return(true);
    }
Beispiel #5
0
        private static string Win32FolderBrowser(string caption)
        {
            ShowCursor();
            IntPtr     bufferAddress = Marshal.AllocHGlobal(256);
            IntPtr     pidl;
            BROWSEINFO browseInfo = new BROWSEINFO()
            {
                hwndOwner = IntPtr.Zero,
                pidlRoot  = IntPtr.Zero,
                lpszTitle = caption,
                ulFlags   = 0x310,
                lParam    = IntPtr.Zero,
                iImage    = 0
            };

            pidl = SHBrowseForFolder(ref browseInfo);
            if (!SHGetPathFromIDList(pidl, bufferAddress))
            {
                // User pressed cancel
                return(null);
            }

            HideCursor();

            return(Marshal.PtrToStringUni(bufferAddress));
        }
Beispiel #6
0
        /// <summary>
        /// It all comes down to this worker method which
        /// browses based on a pidl.
        /// </summary>
        public string BrowseForFolder()
        {
            BROWSEINFO bi = new BROWSEINFO();

            bi.hwndOwner = owner.Handle;
            bi.lpszTitle = title;
            bi.ulFlags   =
                BIF_NEWDIALOGSTYLE |
                BIF_RETURNONLYFSDIRS |
                BIF_DONTGOBELOWDOMAIN |
                BIF_STATUSTEXT;
            bi.pidlRoot = pidlRoot;
            bi.lpfn     = new BrowseCallbackHandler(BrowseCallback);

            IntPtr pidl = SHBrowseForFolder(ref bi);

            if (pidl == (IntPtr)0)
            {
                return(null);
            }

            StringBuilder sb = new StringBuilder(MAX_PATH);

            SHGetPathFromIDList(pidl, sb);
            return(sb.ToString());
        }
Beispiel #7
0
        private void bNew_Click(object sender, EventArgs e) {
            BROWSEINFO bi = new BROWSEINFO();
            bi.hwndOwner = Handle;
            bi.pszDisplayName = "フォルダを選んでください:";

            IntPtr pidl = SHBrowseForFolder(ref bi);
        }
Beispiel #8
0
        public static string BrowseFolder(IntPtr hwndOwner, string title)
        {
            const int  MAX_PATH           = 260;
            const uint BIF_NEWDIALOGSTYLE = 0x40;
            var        bi = new BROWSEINFO();

            bi.hwndOwner      = hwndOwner;
            bi.pidlRoot       = IntPtr.Zero;
            bi.pszDisplayName = new string('\0', MAX_PATH);
            bi.lpszTitle      = title;
            bi.ulFlags        = BIF_NEWDIALOGSTYLE;
            bi.lpfn           = IntPtr.Zero;
            bi.iImage         = 0;
            IntPtr pidl = NativeMethods.SHBrowseForFolder(ref bi);

            if (pidl != IntPtr.Zero)
            {
                try
                {
                    var buff = new StringBuilder(MAX_PATH);
                    if (NativeMethods.SHGetPathFromIDList(pidl, buff))
                    {
                        return(buff.ToString());
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pidl);
                }
            }
            return(null);
        }
Beispiel #9
0
        private void bNew_Click(object sender, EventArgs e)
        {
            BROWSEINFO bi = new BROWSEINFO();

            bi.hwndOwner      = Handle;
            bi.pszDisplayName = "フォルダを選んでください:";

            IntPtr pidl = SHBrowseForFolder(ref bi);
        }
    public DialogResult ShowDialog()
    {
        BROWSEINFO bi = new BROWSEINFO();

        bi.pszDisplayName = IntPtr.Zero;
        bi.lpfn           = IntPtr.Zero;
        bi.lpfn           = BrowserCallBack;
        bi.lParam         = IntPtr.Zero;
        bi.lpszTitle      = "Select Folder";
        IntPtr idListPtr = IntPtr.Zero;
        IntPtr pszPath   = IntPtr.Zero;

        try
        {
            if (m_strTitle.Length != 0)
            {
                bi.lpszTitle = m_strTitle;
            }
            bi.ulFlags        = (int)m_Flags;
            bi.pszDisplayName = Marshal.AllocHGlobal(256);
            // Call SHBrowseForFolder
            idListPtr = Win32SDK.SHBrowseForFolder(bi);
            // Check if the user cancelled out of the dialog or not.
            if (idListPtr == IntPtr.Zero)
            {
                return(DialogResult.Cancel);
            }
            // Allocate ncessary memory buffer to receive the folder path.
            pszPath = Marshal.AllocHGlobal(256);
            // Call SHGetPathFromIDList to get folder path.
            bool bRet = Win32SDK.SHGetPathFromIDList(idListPtr, pszPath);
            // Convert the returned native poiner to string.
            DirectoryPath = Marshal.PtrToStringAuto(pszPath);
            DisplayName   = Marshal.PtrToStringAuto(bi.pszDisplayName);
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
            return(DialogResult.Abort);
        }
        finally
        {
            // Free the memory allocated by shell.
            if (idListPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(idListPtr);
            }
            if (pszPath != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(pszPath);
            }
            Marshal.FreeHGlobal(bi.pszDisplayName);
        }
        return(DialogResult.OK);
    }
Beispiel #11
0
        /// <summary>Shows the dialog box to let the user browse for and select a folder.</summary>
        /// <param name="parentWindowHandle">The HWND of the parent window.</param>
        /// <returns>The selected folder or <c>null</c> if no folder was selected by the user.</returns>
        protected override bool RunDialog(IntPtr parentWindowHandle)
        {
            // Setup BROWSEINFO.dwFlag value
            EnumFlagIndexer <BrowseInfoFlag> browseInfoFlag = BrowseInfoFlag.BIF_SHAREABLE;

            browseInfoFlag[BrowseInfoFlag.BIF_NEWDIALOGSTYLE]      = Application.OleRequired() == ApartmentState.STA;
            browseInfoFlag[BrowseInfoFlag.BIF_DONTGOBELOWDOMAIN]   = HideDomainFolders;
            browseInfoFlag[BrowseInfoFlag.BIF_BROWSEFILEJUNCTIONS] = ShowFileJunctions;
            browseInfoFlag[BrowseInfoFlag.BIF_RETURNONLYFSDIRS]    = LocalFileSystemOnly;
            browseInfoFlag[BrowseInfoFlag.BIF_NONEWFOLDERBUTTON]   = !ShowNewFolderButton;
            browseInfoFlag[BrowseInfoFlag.BIF_EDITBOX | BrowseInfoFlag.BIF_VALIDATE] = ShowFolderPathEditBox;
            switch (BrowseOption)
            {
            case FolderBrowserDialogOptions.Folders:
                break;

            case FolderBrowserDialogOptions.FoldersAndFiles:
                browseInfoFlag |= BrowseInfoFlag.BIF_BROWSEINCLUDEFILES;
                break;

            case FolderBrowserDialogOptions.Computers:
                browseInfoFlag |= BrowseInfoFlag.BIF_BROWSEFORCOMPUTER;
                RootFolder      = defaultComputersFolder;
                break;

            case FolderBrowserDialogOptions.Printers:
                browseInfoFlag |= BrowseInfoFlag.BIF_BROWSEFORPRINTER;
                RootFolder      = defaultPrintersFolder;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            // Setup the BROWSEINFO structure
            var dn = new SafeCoTaskMemString(Kernel32.MAX_PATH);
            var bi = new BROWSEINFO(parentWindowHandle, rootPidl.DangerousGetHandle(), Description, browseInfoFlag, OnBrowseEvent, dn.DangerousGetHandle());

            // Show the dialog
            pidl = SHBrowseForFolder(ref bi);
            href = default(HandleRef);
            if (pidl.IsInvalid)
            {
                return(false);
            }
            if (browseInfoFlag[BrowseInfoFlag.BIF_BROWSEFORPRINTER] || browseInfoFlag[BrowseInfoFlag.BIF_BROWSEFORCOMPUTER])
            {
                SelectedItem = bi.DisplayName;
            }
            else
            {
                SelectedItem = GetNameForPidl(pidl);
            }
            return(true);
        }
Beispiel #12
0
        internal static string FolderBrowser(string caption = "")
        {
            switch (Platform)
            {
            case Platform.Windows:
                IntPtr     bufferAddress = Marshal.AllocHGlobal(256);
                IntPtr     pidl;
                BROWSEINFO browseInfo = new BROWSEINFO()
                {
                    hwndOwner = IntPtr.Zero,
                    pidlRoot  = IntPtr.Zero,
                    lpszTitle = caption,
                    ulFlags   = 0x310,
                    lParam    = IntPtr.Zero,
                    iImage    = 0
                };
                pidl = SHBrowseForFolder(ref browseInfo);
                if (!SHGetPathFromIDList(pidl, bufferAddress))
                {
                    // User pressed cancel
                    return(null);
                }
                return(Marshal.PtrToStringUni(bufferAddress));

            case Platform.Linux:
                IntPtr title = StringToIntPtr(caption);
                IntPtr test  = gtk_file_chooser_dialog_new(title, IntPtr.Zero, 2, IntPtr.Zero);
                g_free(title);

                AddButton(test, "Cancel", -6);
                AddButton(test, "OK", -5);

                string output = null;
                if (gtk_dialog_run(test) == -5)
                {
                    IntPtr response = gtk_file_chooser_get_filename(test);
                    string test2    = GetFileName(response);
                    g_free(response);
                    output = test2;
                }
                gtk_widget_destroy(test);
                while (gtk_events_pending())
                {
                    gtk_main_iteration();
                }
                return(output);

            default:
                return(null);
            }
        }
Beispiel #13
0
    /// <summary>
    /// 选择目录对话框
    /// </summary>
    /// <returns>返回选择的目录路径</returns>
    public static string GetDirectory()
    {
        BROWSEINFO bi      = new BROWSEINFO();
        IntPtr     pidlRet = IntPtr.Zero;

        pidlRet = SHBrowseForFolder(ref bi);
        if (pidlRet == IntPtr.Zero)
        {
            return(string.Empty);
        }
        StringBuilder sb = new StringBuilder(MAX_PATH);

        if (0 == SHGetPathFromIDList(pidlRet, sb))
        {
            return(string.Empty);
        }
        return(sb.ToString());
    }
Beispiel #14
0
    /// <summary>Shows the common folder dialog.</summary>
    /// <param name="hWndOwner">The owner of the folder dialog.</param>
    protected bool RunDialog(IntPtr hWndOwner)
    {
        BROWSEINFO udtBI = new BROWSEINFO();
        IntPtr     lpIDList;
        GCHandle   hTitle = GCHandle.Alloc(Title, GCHandleType.Pinned);

        // set the owner of the window
        udtBI.hWndOwner = hWndOwner;
        // set the owner of the window
        udtBI.lpszTitle = Title;
        // set the owner of the window
        udtBI.ulFlags = (int)BrowseFor;
        // create string buffer for display name
        StringBuilder buffer = new StringBuilder(MAX_PATH);

        buffer.Length        = MAX_PATH;
        udtBI.pszDisplayName = buffer.ToString();
        // show the 'Browse for folder' dialog
        lpIDList = SHBrowseForFolder(ref udtBI);
        hTitle.Free();
        if (lpIDList.ToInt64() != 0)
        {
            if (BrowseFor == BrowseForTypes.Computers)
            {
                m_Selected = udtBI.pszDisplayName.Trim();
            }
            else
            {
                StringBuilder path = new StringBuilder(MAX_PATH);
                // get the path from the IDList
                SHGetPathFromIDList(lpIDList, path);
                m_Selected = path.ToString();
            }
            // free the block of memory
            CoTaskMemFree(lpIDList);
        }
        else
        {
            return(false);
        }
        return(true);
    }
Beispiel #15
0
        static public string BrowseForFolder(string title)
        {
            BROWSEINFO browseInfo = new BROWSEINFO();

            browseInfo.hwndOwner = 0;
            browseInfo.lpszTitle = title;
            browseInfo.ulFlags   = 1;

            uint lpIDList = SHBrowseForFolder(ref browseInfo);

            if (lpIDList != 0)
            {
                StringBuilder path = new StringBuilder(260);
                SHGetPathFromIDList(lpIDList, path);

                CoTaskMemFree(lpIDList);
                return(path.ToString().TrimEnd(new char[] { '\\' }));
            }
            return("");
        }
        /// <summary>
        /// Show dialog
        /// </summary>
        /// <param name="owner"></param>
        /// <returns></returns>
        public bool ShowDialog(Window owner)
        {
            Expect.IsNotNull(owner, nameof(owner));

            StringBuilder sb            = new StringBuilder(256);
            IntPtr        bufferAddress = Marshal.AllocHGlobal(256);;
            IntPtr        pidl          = IntPtr.Zero;
            BROWSEINFO    bi            = new BROWSEINFO();

            bi.hwndOwner = new WindowInteropHelper(owner).Handle;
            bi.pidlRoot  = IntPtr.Zero;

            if (_title != null)
            {
                bi.lpszTitle = _title;
            }
            bi.ulFlags   = _flags;
            bi.lpfn      = OnBrowse;
            bi.lParam    = IntPtr.Zero;
            bi.iImage    = 0;
            SelectedPath = null;

            try
            {
                pidl = SHBrowseForFolder(ref bi);
                if (!SHGetPathFromIDList(pidl, bufferAddress))
                {
                }
                sb.Append(Marshal.PtrToStringAuto(bufferAddress));
            }
            finally
            {
                // Caller is responsible for freeing this memory.
                Marshal.FreeCoTaskMem(pidl);
            }

            SelectedPath = sb.ToString();
            return(SelectedPath.IsNotBlank());
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            string root = Environment.GetFolderPath(RootFolder);

            using (IDLWrapper wrapper = new IDLWrapper(root)) {
                BROWSEINFO bi = new BROWSEINFO();
                bi.hwndOwner = hwndOwner;
                bi.pidlRoot  = wrapper.PIDL;
                bi.lpszTitle = Description;
                bi.ulFlags   = BROWSEINFO.BIF_NEWDIALOGSTYLE | BROWSEINFO.BIF_SHAREABLE | BROWSEINFO.BIF_EDITBOX;
                if (!ShowNewFolderButton)
                {
                    bi.ulFlags |= BROWSEINFO.BIF_NONEWFOLDERBUTTON;
                }

                using (IDLWrapper wrapper2 = new IDLWrapper(PInvoke.SHBrowseForFolder(ref bi))) {
                    SelectedPath = wrapper2.Path;
                    SelectedIDL  = wrapper2.IDL;
                    return(!String.IsNullOrEmpty(SelectedPath));
                }
            }
        }
Beispiel #18
0
        public static string Show(Window parent, string caption, string path)
        {
            var sb = new StringBuilder(MAX_PATH);
            var bi = new BROWSEINFO();

            bi.hwndOwner      = new WindowInteropHelper(parent).Handle;
            bi.pszDisplayName = path;
            bi.lpszTitle      = caption;
            bi.ulFlags        = 0x40;
            bi.lpfn           = (hWnd, msg, lp, wp) =>
            {
                switch (msg)
                {
                case 0x1:
                    SendMessage(new HandleRef(null, hWnd), 0x400 + 103, 1, path);
                    break;
                }

                return(0);
            };

            IntPtr pidl = IntPtr.Zero;

            try
            {
                pidl = SHBrowseForFolder(ref bi);
                if (SHGetPathFromIDList(pidl, sb) == 0)
                {
                    return(null);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(pidl);
            }

            return(sb.ToString());
        }
Beispiel #19
0
		public static string Show(Window parent, string caption, string path)
		{
			var sb = new StringBuilder(MAX_PATH);
			var bi = new BROWSEINFO();
			bi.hwndOwner = new WindowInteropHelper(parent).Handle;
			bi.pszDisplayName = path;
			bi.lpszTitle = caption;
			bi.ulFlags = 0x40;
			bi.lpfn = (hWnd, msg, lp, wp) =>
				{
					switch (msg)
					{
						case 0x1:
							SendMessage(new HandleRef(null, hWnd), 0x400 + 103, 1, path);
							break;
					}

					return 0;
				};

			IntPtr pidl = IntPtr.Zero;
			try
			{
				pidl = SHBrowseForFolder(ref bi);
				if (SHGetPathFromIDList(pidl, sb) == 0)
				{
					return null;
				}
			}
			finally
			{
				Marshal.FreeCoTaskMem(pidl);
			}

			return sb.ToString();
		}
Beispiel #20
0
		private static extern uint SHBrowseForFolder(ref BROWSEINFO browseInfo);
 protected bool RunDialog(IntPtr hWndOwner)
 {
     BROWSEINFO udtBI = new BROWSEINFO();
     IntPtr lpIDList = default(IntPtr);
     GCHandle hTitle = GCHandle.Alloc(Title, GCHandleType.Pinned);
     udtBI.hWndOwner = hWndOwner;
     udtBI.lpszTitle = Title;
     udtBI.ulFlags = (int)BrowseFor;
     StringBuilder buffer = new StringBuilder(MAX_PATH);
     buffer.Length = MAX_PATH;
     udtBI.pszDisplayName = buffer.ToString();
     lpIDList = SHBrowseForFolder(ref udtBI);
     hTitle.Free();
     if (lpIDList.ToInt64() != 0)
     {
         if (BrowseFor == BrowseForTypes.Computers)
         {
             m_Selected = udtBI.pszDisplayName.Trim();
         }
         else
         {
             StringBuilder path = new StringBuilder(MAX_PATH);
             SHGetPathFromIDList(lpIDList, path);
             m_Selected = path.ToString();
         }
         CoTaskMemFree(lpIDList);
     }
     else
     {
         return false;
     }
     return true;
 }
Beispiel #22
0
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            IntPtr zero   = IntPtr.Zero;
            bool   result = false;

            SHGetSpecialFolderLocation(hWndOwner, (int)this.rootFolder, ref zero);
            if (zero == IntPtr.Zero)
            {
                SHGetSpecialFolderLocation(hWndOwner, 0, ref zero);
                if (zero == IntPtr.Zero)
                {
                    throw new InvalidOperationException("FolderBrowserDialogExNoRootFolder");
                }
            }
            int num = 64;

            if (!this.showNewFolderButton)
            {
                num += 512;
            }
            if (Control.CheckForIllegalCrossThreadCalls && Application.OleRequired() != ApartmentState.STA)
            {
                throw new ThreadStateException("ThreadMustBeSTA");
            }
            IntPtr intPtr  = IntPtr.Zero;
            IntPtr intPtr2 = IntPtr.Zero;
            IntPtr intPtr3 = IntPtr.Zero;

            try
            {
                num |= 0x00000010; // BIF_EDITBOX
                num |= 0x00000040; // BIF_NEWDIALOGSTYLE
                BROWSEINFO bROWSEINFO = new BROWSEINFO();
                intPtr2                   = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                intPtr3                   = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                this.callback             = new BrowseCallbackProc(this.FolderBrowserDialogEx_BrowseCallbackProc);
                bROWSEINFO.pidlRoot       = zero;
                bROWSEINFO.hwndOwner      = hWndOwner;
                bROWSEINFO.pszDisplayName = intPtr2;
                bROWSEINFO.lpszTitle      = this.descriptionText;
                bROWSEINFO.ulFlags        = num;
                bROWSEINFO.lpfn           = this.callback;
                bROWSEINFO.lParam         = IntPtr.Zero;
                bROWSEINFO.iImage         = 0;
                intPtr = SHBrowseForFolder(bROWSEINFO);
                if (intPtr != IntPtr.Zero)
                {
                    SHGetPathFromIDList(intPtr, intPtr3);
                    this.selectedPathNeedsCheck = true;
                    this.selectedPath           = Marshal.PtrToStringAuto(intPtr3);
                    result = true;
                }
            }
            finally
            {
                CoTaskMemFree(zero);
                if (intPtr != IntPtr.Zero)
                {
                    CoTaskMemFree(intPtr);
                }
                if (intPtr3 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr3);
                }
                if (intPtr2 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr2);
                }
                this.callback = null;
            }
            return(result);
        }
 public static extern ItemIdList SHBrowseForFolderW(
     ref BROWSEINFO lpbi);
Beispiel #24
0
        public static unsafe extern IntPtr SHBrowseForFolder(
			ref BROWSEINFO lpbi
			);
 public static extern int SHBrowseForFolder(BROWSEINFO mBinf);
Beispiel #26
0
        private string SelectFolder(string caption, IntPtr handle)
        {
            IntPtr     pidl = IntPtr.Zero;
            BROWSEINFO bi   = new BROWSEINFO();

            bi.hwndOwner = handle;
            bi.pidlRoot  = IntPtr.Zero;
            bi.lpszTitle = caption;
            bi.ulFlags   = 0;
            if (this.UseNewStyle)
            {
                bi.ulFlags |= BIF_NEWDIALOGSTYLE;
            }
            if (this.ShowFiles)
            {
                bi.ulFlags |= BIF_BROWSEINCLUDEFILES;
            }
            if (!this.ShowNewFolderButton)
            {
                bi.ulFlags |= BIF_NONEWFOLDERBUTTON;
            }
            if (this.ValidateTextBox)
            {
                bi.ulFlags |= BIF_VALIDATE;
            }
            if (this.ShowTextBox)
            {
                bi.ulFlags |= BIF_EDITBOX;
            }
            bi.lpfn   = new BrowseCallBackProc(OnBrowseEvent);
            bi.lParam = IntPtr.Zero;
            bi.iImage = 0;

            IntPtr bufferAddress = IntPtr.Zero;

            try
            {
                /* Why 520 ???
                 * Longest path possible prior to Windows 10 is 260 characters. and Because of Unicode so 260 * 2 = 520
                 * On Windows 10, there is a policy/settings which enable "Win32 long path"
                 */
                bufferAddress = Marshal.AllocHGlobal(1024);
                this.sb.Clear();
                pidl = SHBrowseForFolder(ref bi);
                if (!SHGetPathFromIDList(pidl, bufferAddress))
                {
                    return(null);
                }
                this.sb.Append(Marshal.PtrToStringAuto(bufferAddress));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Caller is responsible for freeing this memory.
                Marshal.FreeCoTaskMem(pidl);
                if (bufferAddress != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(bufferAddress);
                }
            }

            return(this.sb.ToString());
        }
        public string SelectFolder(string caption, string initialPath, IntPtr parentHandle)
        {
            _initialPath = initialPath;
            StringBuilder sb = new StringBuilder(256);
            IntPtr bufferAddress = Marshal.AllocHGlobal(256); ;
            IntPtr pidl = IntPtr.Zero;
            BROWSEINFO bi = new BROWSEINFO();
            bi.hwndOwner = parentHandle;
            bi.pidlRoot = IntPtr.Zero;
            bi.lpszTitle = caption;
            bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE | BIF_BROWSEINCLUDEFILES;//BIF_NEWDIALOGSTYLE | BIF_SHAREABLE;
            bi.lpfn = new BrowseCallBackProc(OnBrowseEvent);
            bi.lParam = IntPtr.Zero;
            bi.iImage = 0;

            try
            {
                pidl = SHBrowseForFolder(ref bi);
                if (true != SHGetPathFromIDList(pidl, bufferAddress))
                {
                    return null;
                }
                sb.Append(Marshal.PtrToStringAuto(bufferAddress));
            }
            finally
            {
                // Caller is responsible for freeing this memory.
                Marshal.FreeCoTaskMem(pidl);
            }

            return sb.ToString();
        }
Beispiel #28
0
 public static extern CoTaskMemSafeHandle SHBrowseForFolderW(ref BROWSEINFO lpbi);
 public static System.IntPtr SHBrowseForFolder(BROWSEINFO lpbi)
 {
 }
        /// <summary>
        /// When overridden in a derived class, specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>
        /// true if the dialog box was successfully run; otherwise, false.
        /// </returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            IntPtr zero = IntPtr.Zero;
            bool flag = false;
            SHGetSpecialFolderLocation(hwndOwner, (int)rootFolder, ref zero);
            if (zero == IntPtr.Zero)
            {
                SHGetSpecialFolderLocation(hwndOwner, 0, ref zero);
                if (zero == IntPtr.Zero) throw new InvalidOperationException(
                    "Folder Browser Dialog: no root folder");
            }

            //int flags = 0x40;
            //if (!showNewFolderButton) flags += 0x200;

            if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != ApartmentState.STA))
                throw new ThreadStateException("Debugging exception only: Thread must be STA");

            IntPtr pidl = IntPtr.Zero;
            IntPtr hglobal = IntPtr.Zero;
            IntPtr pszPath = IntPtr.Zero;
            try
            {
                BROWSEINFO lpbi = new BROWSEINFO();
                hglobal = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                pszPath = Marshal.AllocHGlobal((int)(260 * Marshal.SystemDefaultCharSize));
                callback = new BrowseCallbackProc(FolderBrowserDialog_BrowseCallbackProc);
                lpbi.pidlRoot = zero;
                lpbi.hwndOwner = hwndOwner;
                lpbi.pszDisplayName = hglobal;
                lpbi.lpszTitle = descriptionText;
                lpbi.ulFlags = (int)flags;
                lpbi.lpfn = callback;
                lpbi.lParam = IntPtr.Zero;
                lpbi.iImage = 0;
                pidl = SHBrowseForFolder(lpbi);
                if (pidl != IntPtr.Zero)
                {
                    SHGetPathFromIDList(pidl, pszPath);
                    selectedPathNeedsCheck = true;
                    selectedPath = Marshal.PtrToStringAuto(pszPath);
                    flag = true;
                }
            }
            finally
            {
                IMalloc sHMalloc = GetSHMalloc();
                sHMalloc.Free(zero);
                if (pidl != IntPtr.Zero) sHMalloc.Free(pidl);
                if (pszPath != IntPtr.Zero) Marshal.FreeHGlobal(pszPath);
                if (hglobal != IntPtr.Zero) Marshal.FreeHGlobal(hglobal);
                
                callback = null;
            }
            return flag;
        }
Beispiel #31
0
 public static extern IntPtr SHBrowseForFolder(ref BROWSEINFO bi);
Beispiel #32
0
        /// <summary>
        /// Shows the folder browser dialog box with the specified owner window.
        /// </summary>
        public DialogResult ShowDialog(IWin32Window owner)
        {
            IntPtr pidlRoot = IntPtr.Zero;

            // Get/find an owner HWND for this dialog.
            IntPtr hWndOwner;

            if (owner != null)
            {
                hWndOwner = owner.Handle;
            }
            else
            {
                hWndOwner = GetActiveWindow();
            }

            // Get the IDL for the specific startLocation.
            SHGetSpecialFolderLocation(hWndOwner, (int)rootLocation, out pidlRoot);

            if (pidlRoot == IntPtr.Zero)
            {
                return DialogResult.Cancel;
            }

            int flags = GetFlags();

            if ((flags & (int)BffStyles.NewDialogStyle) != 0)
            {
                if (System.Threading.ApartmentState.MTA == Application.OleRequired())
                    flags = flags & (~(int)BffStyles.NewDialogStyle);
            }

            IntPtr pidlRet = IntPtr.Zero;

            try
            {
                // Construct a BROWSEINFO.
                BROWSEINFO bi = new BROWSEINFO();
                IntPtr buffer = Marshal.AllocHGlobal(MAX_PATH);

                bi.pidlRoot = pidlRoot;
                bi.hwndOwner = hWndOwner;
                bi.pszDisplayName = buffer;
                bi.lpszTitle = Description;
                bi.ulFlags = flags;
                bi.lpfn = new BFFCALLBACK(FolderBrowserCallback);

                // Show the dialog.
                pidlRet = SHBrowseForFolder(ref bi);

                // Free the buffer you've allocated on the global heap.
                Marshal.FreeHGlobal(buffer);

                if (pidlRet == IntPtr.Zero)
                {
                    // User clicked Cancel.
                    return DialogResult.Cancel;
                }

                // Then retrieve the path from the IDList.
                StringBuilder sb = new StringBuilder(MAX_PATH);
                if (0 == SHGetPathFromIDList(pidlRet, sb))
                {
                    return DialogResult.Cancel;
                }

                // Convert to a string.
                SelectedPath = sb.ToString();
            }
            finally
            {
                IMalloc malloc = GetSHMalloc();
                malloc.Free(pidlRoot);

                if (pidlRet != IntPtr.Zero)
                {
                    malloc.Free(pidlRet);
                }
            }
            return DialogResult.OK;
        }
Beispiel #33
0
 public static extern PIDLIST_ABSOLUTE SHBrowseForFolderW(ref BROWSEINFO lpbi);
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool result = false;

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

            SelectedPath = string.Empty;

            try
            {
                SHGetFolderLocation(hwndOwner, (int)Environment.SpecialFolder.Desktop, IntPtr.Zero, 0, out pidlRoot);

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

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

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

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

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally // release all unmanaged resources
            {
                IMalloc malloc = 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;
        }
Beispiel #35
0
 public static extern IntPtr SHBrowseForFolder(ref BROWSEINFO bi);
 static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lbpi);
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool result = false;

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

            SelectedPath = string.Empty;

            try
            {
                SHGetFolderLocation(hwndOwner, (int)Environment.SpecialFolder.Desktop, IntPtr.Zero, 0, out pidlRoot);

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

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

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

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

                    SelectedPath = Marshal.PtrToStringAuto(pszPath);
                }
            }
            finally // release all unmanaged resources
            {
                IMalloc malloc = 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);
        }
Beispiel #38
0
 public static extern IntPtr SHBrowseForFolder(
     ref BROWSEINFO lbpi);   // Pointer to a BROWSEINFO structure that contains information used to display 
 static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lbpi);
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            IntPtr zero = IntPtr.Zero;
            bool result = false;
            SHGetSpecialFolderLocation(hWndOwner, (int)this.rootFolder, ref zero);
            if (zero == IntPtr.Zero)
            {
                SHGetSpecialFolderLocation(hWndOwner, 0, ref zero);
                if (zero == IntPtr.Zero)
                {
                    throw new InvalidOperationException("FolderBrowserDialogExNoRootFolder");
                }
            }
            int num = 64;
            if (!this.showNewFolderButton)
            {
                num += 512;
            }
            if (Control.CheckForIllegalCrossThreadCalls && Application.OleRequired() != ApartmentState.STA)
            {
                throw new ThreadStateException("ThreadMustBeSTA");
            }
            IntPtr intPtr = IntPtr.Zero;
            IntPtr intPtr2 = IntPtr.Zero;
            IntPtr intPtr3 = IntPtr.Zero;
            try
            {

                num |= 0x00000010; // BIF_EDITBOX
                num |= 0x00000040; // BIF_NEWDIALOGSTYLE
                BROWSEINFO bROWSEINFO = new BROWSEINFO();
                intPtr2 = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                intPtr3 = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                this.callback = new BrowseCallbackProc(this.FolderBrowserDialogEx_BrowseCallbackProc);
                bROWSEINFO.pidlRoot = zero;
                bROWSEINFO.hwndOwner = hWndOwner;
                bROWSEINFO.pszDisplayName = intPtr2;
                bROWSEINFO.lpszTitle = this.descriptionText;
                bROWSEINFO.ulFlags = num;
                bROWSEINFO.lpfn = this.callback;
                bROWSEINFO.lParam = IntPtr.Zero;
                bROWSEINFO.iImage = 0;
                intPtr = SHBrowseForFolder(bROWSEINFO);
                if (intPtr != IntPtr.Zero)
                {
                    SHGetPathFromIDList(intPtr, intPtr3);
                    this.selectedPathNeedsCheck = true;
                    this.selectedPath = Marshal.PtrToStringAuto(intPtr3);
                    result = true;
                }
            }
            finally
            {
                CoTaskMemFree(zero);
                if (intPtr != IntPtr.Zero)
                {
                    CoTaskMemFree(intPtr);
                }
                if (intPtr3 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr3);
                }
                if (intPtr2 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr2);
                }
                this.callback = null;
            }
            return result;
        }
Beispiel #41
0
 [DllImport("shell32")] public static extern int SHBrowseForFolder(BROWSEINFO lpbi);
        /// <summary>
        /// When overridden in a derived class, specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>
        /// true if the dialog box was successfully run; otherwise, false.
        /// </returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            var zero = IntPtr.Zero;
            var flag = false;

            SHGetSpecialFolderLocation(hwndOwner, (int)rootFolder, ref zero);
            if (zero == IntPtr.Zero)
            {
                SHGetSpecialFolderLocation(hwndOwner, 0, ref zero);
                if (zero == IntPtr.Zero)
                {
                    throw new InvalidOperationException(
                              "Folder Browser Dialog: no root folder");
                }
            }

            if (Control.CheckForIllegalCrossThreadCalls && Application.OleRequired() != ApartmentState.STA)
            {
                throw new ThreadStateException("Debugging exception only: Thread must be STA");
            }

            var pidl    = IntPtr.Zero;
            var hglobal = IntPtr.Zero;
            var pszPath = IntPtr.Zero;

            try
            {
                var lpbi = new BROWSEINFO();
                hglobal             = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                pszPath             = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                callback            = new BrowseCallbackProc(FolderBrowserDialog_BrowseCallbackProc);
                lpbi.pidlRoot       = zero;
                lpbi.hwndOwner      = hwndOwner;
                lpbi.pszDisplayName = hglobal;
                lpbi.lpszTitle      = descriptionText;
                lpbi.ulFlags        = (int)CreationFlags;
                lpbi.lpfn           = callback;
                lpbi.lParam         = IntPtr.Zero;
                lpbi.iImage         = 0;
                pidl = SHBrowseForFolder(lpbi);

                if (pidl != IntPtr.Zero)
                {
                    SHGetPathFromIDList(pidl, pszPath);
                    selectedPathNeedsCheck = true;
                    selectedPath           = Marshal.PtrToStringAuto(pszPath);
                    flag = true;
                }
            }
            finally
            {
                var sHMalloc = GetSHMalloc();
                sHMalloc.Free(zero);
                if (pidl != IntPtr.Zero)
                {
                    sHMalloc.Free(pidl);
                }
                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }
                if (hglobal != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hglobal);
                }

                callback = null;
            }

            return(flag);
        }
Beispiel #43
0
		static public string BrowseForFolder(string title)
		{
			BROWSEINFO browseInfo = new BROWSEINFO();
			browseInfo.hwndOwner = 0;
			browseInfo.lpszTitle = title;
			browseInfo.ulFlags = 1;

			uint lpIDList = SHBrowseForFolder(ref browseInfo);
			if (lpIDList != 0)
			{
				StringBuilder path = new StringBuilder(260);
				SHGetPathFromIDList(lpIDList, path);

				CoTaskMemFree(lpIDList);
				return  path.ToString().TrimEnd(new char[]{'\\'});
			}
			return "";
		}
Beispiel #44
0
 private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
 internal static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
Beispiel #46
0
 public static extern IntPtr SHBrowseForFolder(
     ref BROWSEINFO lbpi);       // Pointer to a BROWSEINFO structure that contains information used to display
Beispiel #47
0
 private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO bi);
Beispiel #48
0
 internal static extern IntPtr SHBrowseForFolder([In] BROWSEINFO lpbi);
 private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpBrowseInfo);