private string SelectFolder(string caption, string initialPath, IntPtr parentHandle) { this.initialPath = initialPath; StringBuilder sb = new StringBuilder(256); IntPtr bufferAddress = Marshal.AllocHGlobal(256);; IntPtr pidl = IntPtr.Zero; var bi = new BROWSEINFO(); bi.hwndOwner = parentHandle; bi.pidlRoot = IntPtr.Zero; bi.lpszTitle = caption; bi.ulFlags = Shell32.BIF_NEWDIALOGSTYLE | Shell32.BIF_SHAREABLE; bi.lpfn = new BrowseCallBackProc(OnBrowseEvent); bi.lParam = IntPtr.Zero; bi.iImage = 0; try { pidl = Shell32.SHBrowseForFolder(ref bi); if (true != Shell32.SHGetPathFromIDList(pidl, bufferAddress)) { return(null); } sb.Append(Marshal.PtrToStringAuto(bufferAddress)); } finally { // Caller is responsible for freeing this memory. Marshal.FreeCoTaskMem(pidl); } return(sb.ToString()); }
/// <summary> /// Displays the "open folder" dialog and returns the selected path name. /// </summary> /// <param name="title">Title of dialog.</param> /// <returns>The path of selected folder.</returns> public static string OpenFolderDialog(string title) { var lpbi = new BrowseInfo { lpszTitle = title, ulFlags = BIFlags.BIF_RETURNONLYFSDIRS }; var pidl = Shell32.SHBrowseForFolder(lpbi); var pszPath = new char[256]; if (Shell32.SHGetPathFromIDList(pidl, pszPath)) { var fdrPath = new string(pszPath); return(fdrPath.Substring(0, fdrPath.IndexOf('\0'))); } return(string.Empty); }
private void btnBrowseMachine_Click(object sender, EventArgs e) { IntPtr pidlRet = IntPtr.Zero; IntPtr pidlRoot = IntPtr.Zero; Shell32.SHGetSpecialFolderLocation(this.Handle, (int)Shell32.FolderID.NetworkNeighborhood, out pidlRoot); try { Shell32.BROWSEINFO bi = new Shell32.BROWSEINFO(); IntPtr buffer = Marshal.AllocHGlobal(Win32Utils.MAX_PATH); bi.hwndOwner = this.Handle; bi.pidlRoot = pidlRoot; bi.lpszTitle = "Browse for Machine"; bi.pszDisplayName = buffer; bi.ulFlags = (int)(Shell32.BrowseInfoFlags.BIF_BROWSEFORCOMPUTER | Shell32.BrowseInfoFlags.BIF_NONEWFOLDERBUTTON); pidlRet = Shell32.SHBrowseForFolder(ref bi); if (pidlRet != IntPtr.Zero) { string machineName = Marshal.PtrToStringAuto(bi.pszDisplayName).ToUpper(); cboMachine.Text = machineName; InsertRecentMachine(machineName); } // Free the buffer we've allocated on the global heap Marshal.FreeHGlobal(buffer); } finally { Shell32.IMalloc malloc; Shell32.SHGetMalloc(out malloc); malloc.Free(pidlRoot); if (pidlRet != IntPtr.Zero) { malloc.Free(pidlRet); } } }
/// <summary> /// Displays the folder browser dialog. /// </summary> /// <param name="hwndOwner">Handle to the window that owns the dialog box.</param> /// <returns> /// If the user clicks the OK button of the dialog that is displayed, true is returned; otherwise, false. /// </returns> protected override bool RunDialog(IntPtr hwndOwner) { var result = false; IntPtr pidlRoot = IntPtr.Zero, pszPath = IntPtr.Zero, pidlSelected = IntPtr.Zero; SelectedPath = string.Empty; try { if (UseSpecialFolderRoot) { Shell32.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, IntPtr.Zero, 0, out pidlRoot); } else // RootType == Path { uint iAttribute; Shell32.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute); } var browseInfo = new BrowseInfo { HwndOwner = hwndOwner, Root = pidlRoot, DisplayName = new string(' ', 256), Title = Title, Flags = _dialogOptions, LParam = 0, Callback = HookProc }; // Show dialog pidlSelected = Shell32.SHBrowseForFolder(ref browseInfo); if (pidlSelected != IntPtr.Zero) { result = true; pszPath = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize); Shell32.SHGetPathFromIDList(pidlSelected, pszPath); SelectedPath = Marshal.PtrToStringAuto(pszPath); } } finally // release all unmanaged resources { var 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); }