如何用C ++打开文件夹

我需要打开一个显示特定文件夹的资源管理器窗口,让我们说"C:\\Windows"我应该用什么function来实现我的目标? 我正在使用Windows所以可以使用API​​,我也可以使用boost,但我不能使用C ++ 11。

您可以使用SHOpenFolderAndSelectItems函数来执行此操作,而不是自己强行运行资源管理器(例如,如果用户将Explorer替换为其默认文件管理器,该怎么办?)。

 LPCWSTR pszPathToOpen = L"C:\\Windows"; PIDLIST_ABSOLUTE pidl; if (SUCCEEDED(SHParseDisplayName(pszPathToOpen, 0, &pidl, 0, 0))) { // we don't want to actually select anything in the folder, so we pass an empty // PIDL in the array. if you want to select one or more items in the opened // folder you'd need to build the PIDL array appropriately ITEMIDLIST idNull = { 0 }; LPCITEMIDLIST pidlNull[1] = { &idNull }; SHOpenFolderAndSelectItems(pidl, 1, pidlNull, 0); ILFree(pidl); } 

或者,您可以直接在文件夹上调用ShellExecute来运行其默认操作(通常在浏览器窗口中打开):

 ShellExecute(NULL, NULL, L"C:\\Windows", NULL, NULL, SW_SHOWNORMAL); 

一小时前我刚写了类似的function。

此function不能100%执行,但您可以使用它来获得所需function。 它会打开资源管理器窗口并标记您指向的文件。 假设您指定了"C:\Windows\System32"在这种情况下,您将打开"C:\Windows"并标记System32 。 如果你想进去,你需要使用像FindFirstFile这样的东西。 如果目录为空,我提供的解决方案将无效…

 bool ExplorerGoTo (const String &Path) { TCHAR tcBuff[8] = {0}; lstrcpyn(tcBuff, Path.c_str(), 5); String stParams = _T("/n, /select, "); if( lstrcmpi(_T("\\??\\"), tcBuff) == 0 ) { stParams += (Path[4]); } else { stParams += Path; } String stExplorer = _T("C:\\Windows\\explorer.exe"); //ExpandPath(stExplorer); if (stExplorer.empty ()) stExplorer = _T("explorer.exe"); SHELLEXECUTEINFO shi = { 0 }; shi.cbSize = sizeof (SHELLEXECUTEINFO); shi.lpVerb = _T("open"); shi.lpFile = stExplorer.c_str (); shi.lpParameters = stParams.c_str (); shi.nShow = SW_SHOW; bool bRes = ShellExecuteEx( &shi ); if( bRes == FALSE && GetLastError() != 0 ) { Sleep(200); return ShellExecuteEx( &shi ); } return bRes; } 

永远不要使用system()