public DialogResult ShowDialog() { BrowseInfo bi = new BrowseInfo(); bi.pszDisplayName = IntPtr.Zero; bi.lpfn = IntPtr.Zero; bi.lParam = IntPtr.Zero; bi.lpszTitle = "Select Folder"; IntPtr idListPtr = IntPtr.Zero; IntPtr pszPath = IntPtr.Zero; try { if (this.m_strTitle.Length != 0) { bi.lpszTitle = this.m_strTitle; } bi.ulFlags = (int)this.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); this.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() { BrowseInfo bi = new BrowseInfo(); bi.pszDisplayName = IntPtr.Zero; bi.lpfn = IntPtr.Zero; bi.lParam = IntPtr.Zero; bi.lpszTitle = "Select Folder"; IntPtr idListPtr = IntPtr.Zero; IntPtr pszPath = IntPtr.Zero; try { if (this.m_strTitle.Length != 0) { bi.lpszTitle = this.m_strTitle; } bi.ulFlags = (int)this.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); this.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);