//在资源管理器文件的定位 BOOL OpenFolderAndSelectFile( LPTSTR lpszFilePath) { // // GetFolder // DWORD dw = lstrlen(lpszFilePath) - 1; for (;dw != -1;dw--) { if ( lpszFilePath[dw] == '\\') { break; } } if ( dw == -1) { return FALSE; } // // Get a pointer to the Desktop's IShellFolder interface. // LPSHELLFOLDER pDesktopFolder; if ( SUCCEEDED(SHGetDesktopFolder( &pDesktopFolder))) { // Convert the path to an ITEMIDLIST. LPITEMIDLIST pidl; ULONG chEaten; ULONG dwAttributes; HRESULT hr; lpszFilePath[dw] = '\0'; hr = pDesktopFolder->ParseDisplayName(NULL, NULL, lpszFilePath, &chEaten, &pidl, &dwAttributes); if (FAILED(hr)) { pDesktopFolder->Release(); return FALSE; } LPCITEMIDLIST pidlFolder = pidl; lpszFilePath[dw] = '\\'; hr = pDesktopFolder->ParseDisplayName(NULL, NULL, lpszFilePath, &chEaten, &pidl, &dwAttributes); if (FAILED(hr)) { pDesktopFolder->Release(); return FALSE; } LPCITEMIDLIST pidlFile = pidl; TRACE(_T("%d %d\n"),pidlFolder, pidlFile); CoInitialize( NULL); hr = SHOpenFolderAndSelectItems( pidlFolder, 1, &pidlFile, 0); CoUninitialize(); pDesktopFolder->Release(); if ( hr == S_OK) return TRUE; } return FALSE; } //使用 CString str(_T(“D:\\visual studio 2010\\Projects\\1.txt”)); OpenFolderAndSelectFile(str.GetBuffer(str.GetLength())); //另一方法 explorer命令行用法 Explorer [/n] [/e] [(,)/root,<object>] [/select,<object>] /n Opens a new single-pane window for the default selection. This is usually the root of the drive Windows is installed on. If the window is already open, a duplicate opens. /e Opens Windows Explorer in its default view. /root,<object> Opens a window view of the specified object. /select,<object> Opens a window view with the specified folder, file or application selected. CString str; str.Format(TEXT("/select,%s "), m_strFile); ShellExecute(NULL, TEXT("open"), TEXT("explorer.exe"), str, NULL, SW_SHOWNORMAL); |