打开文件夹

 1 BOOL OpenFilePath( LPCTSTR lpszFile )
 2 {
 3     HINSTANCE hInst;
 4     HRESULT hr;
 5     LPITEMIDLIST pidFile;
 6     TCHAR szFile[MAX_PATH];
 7     USES_CONVERSION;
 8 
 9     if ( lpszFile == NULL || *lpszFile == 0 )
10     {
11         return FALSE;
12     }
13 
14     memcpy( szFile, lpszFile, sizeof( szFile ) );
15 
16     if ( ::PathFileExists( szFile ) )
17     {
18         hr = ::SHParseDisplayName( T2W( szFile ), NULL, &pidFile, 0, NULL );
19         if ( hr != S_OK )
20         {
21             return FALSE;
22         }
23 
24         hr = ::SHOpenFolderAndSelectItems( pidFile, 0, NULL, 0 );
25         if ( hr != S_OK )
26         {
27             ::CoTaskMemFree( pidFile );
28             return FALSE;
29         }
30 
31         ::CoTaskMemFree( pidFile );
32     }
33     else
34     {
35         ::PathRemoveFileSpec( szFile );
36 
37         if ( !::PathIsDirectory( szFile ) )
38         {
39             return FALSE;
40         }
41 
42         hInst = ::ShellExecute( NULL, NULL, NULL, NULL, szFile, SW_SHOWNORMAL );
43         if ( ( DWORD )hInst <= 32 )
44         {
45             return FALSE;
46         }
47     }
48 
49     return TRUE;
50 }

 

posted on 2012-12-09 12:04  郁郁思扬  阅读(247)  评论(0编辑  收藏  举报

导航