WinExec和ShellExecute都能打开文件夹。不过用sjdev的比较好,刚查了下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.
打开某文件夹void OpenDirect( char *dir ) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); si.dwFlags |= STARTF_USESHOWWINDOW; si.wShowWindow = SW_SHOW; ZeroMemory( &pi, sizeof( pi ) ); char file[ 300 ] = "Explorer.exe "; strcat( file, dir ); if( !CreateProcess( NULL, // No module name (use command line). file, // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { AfxMessageBox( "CreateProcess failed." ); } }
SHOpenFolderAndSelectItems Function--------------------------------------------------------------------------------Opens a Microsoft® Windows® Explorer window with specified items in a particular folder selected.
dlg.Domodal();
strFile.Format(" /e,/select,%s",FileName);
ShellExecute(NULL,"open","explorer.exe",(LPCTSTR)strFile,NULL,true);
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.
OpenDirect( char *dir )
{
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
ZeroMemory( &pi, sizeof( pi ) ); char file[ 300 ] = "Explorer.exe ";
strcat( file, dir );
if( !CreateProcess( NULL, // No module name (use command line).
file, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
AfxMessageBox( "CreateProcess failed." );
}
}
//#include "stdafx.h"
#include <shlobj.h>#include <tchar.h>
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <process.h> #include <conio.h>#include <time.h>#include <cstdio>#include <stdarg.h>#include <signal.h>#include <stdlib.h>
#include <string.h>HRESULT GetShellFolderViewDual(ITEMIDLIST* pidl, IShellFolderViewDual** ppIShellFolderViewDual)
{ IWebBrowserApp* pIWebBrowserApp; IDispatch* pDoc; HWND hWnd; HRESULT hr; HINSTANCE ghSHDOCVW;
HRESULT(WINAPI * gpfSHGetIDispatchForFolder)(ITEMIDLIST * pidl, IWebBrowserApp * *ppIWebBrowserApp); *ppIShellFolderViewDual = NULL; ghSHDOCVW = LoadLibrary(_T("SHDOCVW.DLL")); if(ghSHDOCVW == NULL) return FALSE; pIWebBrowserApp = NULL; gpfSHGetIDispatchForFolder = (HRESULT(WINAPI *)(ITEMIDLIST *, IWebBrowserApp * *)) GetProcAddress(ghSHDOCVW, "SHGetIDispatchForFolder "); if(gpfSHGetIDispatchForFolder == NULL) return FALSE; if(SUCCEEDED(gpfSHGetIDispatchForFolder(pidl, &pIWebBrowserApp))) { if(SUCCEEDED(pIWebBrowserApp-> get_HWND((long *) &hWnd))) { SetForegroundWindow(hWnd); ShowWindow(hWnd, SW_SHOWNORMAL); } if(SUCCEEDED(hr = pIWebBrowserApp-> get_Document(&pDoc))) { pDoc-> QueryInterface(IID_IShellFolderViewDual, (void * *) ppIShellFolderViewDual); pDoc-> Release(); } pIWebBrowserApp-> Release(); } FreeLibrary(ghSHDOCVW); return TRUE;}
BOOL XZSHOpenFolderAndSelectItems(ITEMIDLIST* pidlFolder) { HRESULT(WINAPI * gpfSHOpenFolderAndSelectItems)(LPCITEMIDLIST * pidlFolder, UINT cidl, LPCITEMIDLIST * apidl, DWORD dwFlags); HINSTANCE ghShell32; ghShell32 = LoadLibrary(_T("Shell32.DLL")); if(ghShell32 == NULL) { return FALSE; } gpfSHOpenFolderAndSelectItems = (HRESULT(WINAPI *)(LPCITEMIDLIST *, UINT, LPCITEMIDLIST *, DWORD)) GetProcAddress(ghShell32, "SHOpenFolderAndSelectItems"); if(gpfSHOpenFolderAndSelectItems != NULL) { if(SUCCEEDED(gpfSHOpenFolderAndSelectItems((LPCITEMIDLIST *) pidlFolder, 0, (LPCITEMIDLIST *) NULL, 0))) { FreeLibrary(ghShell32); return TRUE; } FreeLibrary(ghShell32); return FALSE; } FreeLibrary(ghShell32); return FALSE;} void FindTarget(LPCTSTR str) { HRESULT hres; IShellLink* psl; ITEMIDLIST* pidl; IPersistFile* ppf; CoInitialize(NULL); printf("11111111, err=%d\n", GetLastError()); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if(SUCCEEDED(hres)) { printf("222222222, err=%d\n", GetLastError()); psl-> SetPath(str); printf("666666666 , err=%d\n", GetLastError()); psl-> GetIDList(&pidl); hres = psl-> QueryInterface(IID_IPersistFile, (void * *) &ppf); if(SUCCEEDED(hres)) { printf("3333333333, err=%d\n", GetLastError()); WCHAR wsz[MAX_PATH];#ifdef _UNICODE wcscpy(wsz, str); #else MultiByteToWideChar(CP_ACP, 0, str, -1, wsz, MAX_PATH); #endif // Load the shortcut. hres = ppf-> Load(wsz, STGM_READ); if(SUCCEEDED(hres)) { printf("444444444, err=%d\n", GetLastError()); psl-> GetIDList(&pidl); } ppf-> Release(); } printf("55555555555, err=%d\n", GetLastError()); XZSHOpenFolderAndSelectItems(pidl);
psl-> Release(); } CoUninitialize();}
int main(int argc, char* argv[]) {
FindTarget(("c:\\temp")); return 0;}
FutureCode posted @ Sat, 26 Feb 2011 22:45:16 +0800 in Chapter 0 - Others , 123 readers
假设在C:\目录下存在文件a.txt。打开这个目录是ShellExecute的常用功能,代码如下: ShellExecute(NULL, "open", "explorer.exe", "C:\\", NULL, SW_SHOWNORMAL);要在此基础上增加“选中文件”功能(这是很多下载工具和格式转换工具提供的常用功能),只需要将上面目录的名称换成文件的名称,并在前面加上"/select,": ShellExecute(NULL, "open", "explorer.exe", "/select,C:\\a.txt", NULL, SW_SHOWNORMAL);