我需要打开一个显示特定文件夹的资源管理器窗口,让我说"C:\\Windows"
我应该使用什么功能来达到我的目标?我使用的是 Windows,所以可以使用 API,也可以使用 boost,但我不能使用 C++11。
2 回答
您可以使用该SHOpenFolderAndSelectItems
功能来执行此操作,而不是自己强制运行资源管理器(例如,如果用户已将资源管理器替换为其默认文件管理器怎么办?)。
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);
An hour ago I just wrote similar function.
This function doesn't do 100% as you want, but you can use it to get that you want.
It opens explorer window and marks file you are pointing to. Lets say you specified "C:\Windows\System32"
in this case you will have "C:\Windows"
opened and System32
marked. If you want to go inside you need to use something like FindFirstFile
. If directory is empty, my offered solution wouldn't work...
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;
}
And never use system()