Ejemplo n.º 1
0
    public bool ShowDialog(IWin32Window owner)
    {
        _path = string.Empty;
        IntPtr handle;
        IntPtr zero = IntPtr.Zero;

        if (owner != null)
        {
            handle = owner.Handle;
        }
        else
        {
            handle = UnmanagedMethods.GetActiveWindow();
        }
        UnmanagedMethods.SHGetSpecialFolderLocation(handle, (int)_startLocation, ref zero);
        if (zero == IntPtr.Zero)
        {
            return(false);
        }
        int num = (int)_options;

        if ((num & 0x40) != 0)
        {
            Application.OleRequired();
        }
        IntPtr pidl = IntPtr.Zero;

        try
        {
            BrowseInfo lpbi = new BrowseInfo();
            //IntPtr pszPath = Marshal.AllocHGlobal(MAX_PATH);
            lpbi.pidlRoot    = zero;
            lpbi.hwndOwner   = handle;
            lpbi.displayName = new string('\0', MAX_PATH);
            lpbi.title       = _title;
            lpbi.flags       = num;
            lpbi.callback    = null;
            lpbi.lparam      = IntPtr.Zero;
            pidl             = UnmanagedMethods.SHBrowseForFolder(ref lpbi);
            if (pidl == IntPtr.Zero)
            {
                return(false);
            }
            _displayName = lpbi.displayName;
            StringBuilder pathReturned = new StringBuilder(MAX_PATH);
            UnmanagedMethods.SHGetPathFromIDList(pidl, pathReturned);
            _path = pathReturned.ToString();
            UnmanagedMethods.SHMemFree(pidl);
        }
        finally
        {
            UnmanagedMethods.SHMemFree(zero);
        }
        return(true);
    }