如何获得SHBrowseForFolder函数的完整path?

我正在使用SHBrowseForFolder和SHGetPathFromIDList函数来获取用户选定的文件夹path。 但是,此方法不会返回完整path的驱动器path。 如何额外获得这些信息呢?

从这个新闻组帖子采取:

您可以使用SHBrowseForFolder(…),它将BROWSEINFO作为参数;

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返回文件夹的PIDL及其显示名称,从PIDL获取完整路径,调用SHGetPathFromIDList

编辑: OP似乎有麻烦得到它的工作,所以这里有一些工作的C#代码(你应该能够把它翻译成任何语言,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(); } }