private int BrowserCallBack(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam)
 {
     if (uMsg == (int)BrowseForFolderMessages.BFFM_INITIALIZED)
     {
         Win32SDK.SendMessage(hWnd, (int)BrowseForFolderMessages.BFFM_SETSELECTIONW, 1, m_initDir);
     }
     return(0);
 }
    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);
    }