1,目的
介紹獲取Windows常用的一些系統路徑的方法(當前用戶),如:桌面、我的文檔等。
並提供簡單封裝為類PathHelper,供以后工程快捷調用。
2,原理&實現
第一部分 常用系統及進程目錄
①獲取系統system32路徑:(c:\windows\system32)
使用函數GetSystemDirectory. CString版封裝:
- static CString GetSysFolder ()
- {
- TCHAR szPath[100] ={0};
- GetSystemDirectory( szPath, 100 ) ;
- return CString(szPath) ;
- }
- static CString GetWindowsFolder()
- {
- TCHAR szPath[100] ={0};
- GetWindowsDirectory( szPath, 100 ) ;
- return CString(szPath) ;
- }
③獲取模塊運行所在文件夾(exe所在目錄)
- //獲取運行目錄(exe所在目錄)
- static CString GetModuleFolder( HMODULE hModule )
- {
- TCHAR szPath[MAX_PATH] = {0} ;
- GetModuleFileName( hModule, szPath, MAX_PATH ) ;
- ZeroMemory(_tcsrchr(szPath,_T('\\')), _tcslen(_tcsrchr(szPath,_T('\\') ) )*sizeof(TCHAR)) ;
- return CString(szPath) ;
- }
④獲取模塊文件完整路徑(包含exe文件名及后綴)
- //獲取運行模塊文件全路徑
- static CString GetModuleFilePath( HMODULE hModule )
- {
- TCHAR szPath[MAX_PATH] = {0} ;
- GetModuleFileName( hModule, szPath, MAX_PATH ) ;
- return CString(szPath) ;
- }
⑤上級目錄:
- //得到上一級目錄
- static CString GetUpFolder(CString strPath)
- {
- int nPos = strPath.ReverseFind('\\');
- return strPath.Left(nPos);
- }
附:PathHelper 小工具類頭文件:
- /*
- 提供簡化的路徑操作:如exe路徑、系統路徑、上級目錄
- */
- #ifndef PATH_HELPER_H
- #define PATH_HELPER_H
- class PathHelper
- {
- public:
- PathHelper(){}
- ~PathHelper(){}
- //得到上一級目錄
- static CString GetUpFolder(CString strPath)
- {
- int nPos = strPath.ReverseFind('\\');
- return strPath.Left(nPos);
- }
- //獲取運行目錄(exe所在目錄)
- static CString GetModuleFolder( HMODULE hModule )
- {
- TCHAR szPath[MAX_PATH] = {0} ;
- GetModuleFileName( hModule, szPath, MAX_PATH ) ;
- ZeroMemory(_tcsrchr(szPath,_T('\\')), _tcslen(_tcsrchr(szPath,_T('\\') ) )*sizeof(TCHAR)) ;
- return CString(szPath) ;
- }
- //獲取運行模塊文件全路徑
- static CString GetModuleFilePath( HMODULE hModule )
- {
- TCHAR szPath[MAX_PATH] = {0} ;
- GetModuleFileName( hModule, szPath, MAX_PATH ) ;
- return CString(szPath) ;
- }
- static CString GetSysFolder ()
- {
- TCHAR szPath[100] ={0};
- GetSystemDirectory ( szPath, 100 ) ;
- return CString(szPath) ;
- }
- static CString GetWindowsFolder()
- {
- TCHAR szPath[100] ={0};
- GetWindowsDirectory ( szPath, 100 ) ;
- return CString(szPath) ;
- }
- };
- #endif
第二部分 系統特殊路徑
使用API:
BOOL SHGetSpecialFolderPath( HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate );
參數說明:
HWND hwndOwner:如果在一個對話框或messagebox上顯示時,用到的窗口句柄。這里我們單純獲取路徑,一般設NULL。
LPTSTR lpszPath : 接收路徑的字符串
int nFolder : 微軟定義好的一個標志,用到代表要獲取那種目錄。
常見的如下:
- CSIDL_BITBUCKET 回收站
- CSIDL_CONTROLS 控制面板
- CSIDL_DESKTOP Windows 桌面Desktop
- CSIDL_DESKTOPDIRECTORY Desktop的目錄
- CSIDL_DRIVES 我的電腦
- CSIDL_FONTS 字體目錄
- CSIDL_NETHOOD 網上鄰居
- CSIDL_NETWORK 網上鄰居虛擬目錄
- CSIDL_PERSONAL 我的文檔
- CSIDL_PRINTERS 打印機
- CSIDL_PROGRAMS 程序組
- CSIDL_RECENT 最近打開的文檔
- CSIDL_SENDTO “發送到”菜單項
- CSIDL_STARTMENU 任務條啟動菜單項
- CSIDL_STARTUP 啟動目錄
- CSIDL_TEMPLATES 文檔模板
具體內容可以參照 shlobj.h 。
BOOL fCreate : 如果該文件夾不存在,是否創建它。 一般我們不改系統文件夾,就設FASLE。
調用示例:
- CHAR szPath[MAX_PATH] = {0};
- SHGetSpecialFolderPath(NULL, szPath,CSIDL_DESKTOP, FALSE);
- MessageBox(szPath);