How to get the full path from the SHBrowseForFolder function?

I use the SHBrowseForFolder and SHGetPathFromIDList functions to get the selected folder path to the user. However, this method does not return the path to the path of the full path. How to get this information in addition?

+3
source share
1 answer

Taken from this post newsgroup :

You can use SHBrowseForFolder (...), it takes a BROWSEINFO parameter as a parameter;

TCHAR szDir[MAX_PATH];
BROWSEINFO bInfo;
bInfo.hwndOwner = Owner window
bInfo.pidlRoot = NULL; 
bInfo.pszDisplayName = szDir; // Address of a buffer to receive the display name of the folder selected by the user
bInfo.lpszTitle = "Please, select a folder"; // Title of the dialog
bInfo.ulFlags = 0 ;
bInfo.lpfn = NULL;
bInfo.lParam = 0;
bInfo.iImage = -1;

LPITEMIDLIST lpItem = SHBrowseForFolder( &bInfo);
if( lpItem != NULL )
{
  SHGetPathFromIDList(lpItem, szDir );
  //......
}

SHBrowseForFolder returns the PIDL folder and its display name, to get the full path from PIDL, call SHGetPathFromIDList

: , OP , # ( , API- ):

class SHGetPath
{
    [DllImport("shell32.dll")]
    static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);

    [DllImport("shell32.dll")]
    public static extern Int32 SHGetPathFromIDList(
    IntPtr pidl, StringBuilder pszPath);

    public delegate int BrowseCallBackProc(IntPtr hwnd, int msg, IntPtr lp, IntPtr wp);
    struct BROWSEINFO 
    {
        public IntPtr hwndOwner;
        public IntPtr pidlRoot;
        public string pszDisplayName;
        public string lpszTitle;
        public uint ulFlags;
        public BrowseCallBackProc lpfn;
        public IntPtr lParam;
        public int iImage;
    }

    public SHGetPath()
    {
        Console.WriteLine(SelectFolder("Hello World", "C:\\"));
    }

    public string SelectFolder(string caption, string initialPath)
    {
        StringBuilder sb = new StringBuilder(256);
        IntPtr pidl = IntPtr.Zero;
        BROWSEINFO bi;
        bi.hwndOwner = Process.GetCurrentProcess().MainWindowHandle; ;
        bi.pidlRoot = IntPtr.Zero;
        bi.pszDisplayName = initialPath;
        bi.lpszTitle = caption;
        bi.ulFlags = 0; // BIF_NEWDIALOGSTYLE | BIF_SHAREABLE;
        bi.lpfn = null; // new BrowseCallBackProc(OnBrowseEvent);
        bi.lParam = IntPtr.Zero;
        bi.iImage = 0;

        try
        {
            pidl = SHBrowseForFolder(ref bi);
            if (0 == SHGetPathFromIDList(pidl, sb))
            {
                return null;
            }
        }
        finally
        {
            // Caller is responsible for freeing this memory.
            Marshal.FreeCoTaskMem(pidl);
        }
        return sb.ToString();
    }
}
+5

Source: https://habr.com/ru/post/1726459/


All Articles