public DialogResult ShowDialog() { var bi = new BROWSEINFO { pszDisplayName = IntPtr.Zero, lpfn = IntPtr.Zero, lParam = IntPtr.Zero, 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. m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath); m_strDisplayName = 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); } if (bi != null) { Marshal.FreeHGlobal(bi.pszDisplayName); } } return(DialogResult.OK); }
public DialogResult ShowDialog() { var bi = new BROWSEINFO { pszDisplayName = IntPtr.Zero, lpfn = IntPtr.Zero, lParam = IntPtr.Zero, 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. m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath); m_strDisplayName = 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); } if (bi != null) { Marshal.FreeHGlobal(bi.pszDisplayName); } } return DialogResult.OK; }
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);