(轉)c++--【路徑相關方法】1,獲取各種系統路徑


 

1,目的

 

介紹獲取Windows常用的一些系統路徑的方法(當前用戶),如:桌面、我的文檔等。

並提供簡單封裝為類PathHelper,供以后工程快捷調用。

 

2,原理&實現

 

第一部分 常用系統及進程目錄

 

①獲取系統system32路徑:(c:\windows\system32)

 使用函數GetSystemDirectory.  CString版封裝:

 

[cpp]  view plain copy
  1. static CString GetSysFolder ()  
  2.     {  
  3.         TCHAR szPath[100] ={0};  
  4.         GetSystemDirectory( szPath, 100 ) ;  
  5.         return CString(szPath) ;  
  6.     }  
②獲取系統windows路徑:(c:\windows)

 

 

[cpp]  view plain copy
  1. static CString GetWindowsFolder()  
  2.     {  
  3.         TCHAR szPath[100] ={0};  
  4.         GetWindowsDirectory( szPath, 100 ) ;  
  5.         return CString(szPath) ;  
  6.     }  

 

③獲取模塊運行所在文件夾(exe所在目錄)

 

[cpp]  view plain copy
  1. //獲取運行目錄(exe所在目錄)  
  2.     static CString GetModuleFolder( HMODULE hModule )  
  3.     {  
  4.         TCHAR   szPath[MAX_PATH] = {0} ;  
  5.         GetModuleFileName( hModule, szPath, MAX_PATH ) ;  
  6.         ZeroMemory(_tcsrchr(szPath,_T('\\')), _tcslen(_tcsrchr(szPath,_T('\\') ) )*sizeof(TCHAR)) ;  
  7.         return CString(szPath) ;  
  8.     }  


 

④獲取模塊文件完整路徑(包含exe文件名及后綴)

 

[cpp]  view plain copy
  1. //獲取運行模塊文件全路徑  
  2.     static CString GetModuleFilePath( HMODULE hModule )  
  3.     {  
  4.         TCHAR   szPath[MAX_PATH] = {0} ;  
  5.         GetModuleFileName( hModule, szPath, MAX_PATH ) ;  
  6.         return CString(szPath) ;  
  7.     }  

⑤上級目錄:

 

[cpp]  view plain copy
  1. //得到上一級目錄  
  2.     static CString GetUpFolder(CString strPath)  
  3.     {  
  4.         int nPos = strPath.ReverseFind('\\');  
  5.         return strPath.Left(nPos);  
  6.     }  

 

 

附:PathHelper 小工具類頭文件:

 

 

[cpp]  view plain copy
  1. /* 
  2. 提供簡化的路徑操作:如exe路徑、系統路徑、上級目錄 
  3. */  
  4.   
  5.   
  6. #ifndef PATH_HELPER_H  
  7. #define PATH_HELPER_H  
  8.   
  9. class PathHelper  
  10. {  
  11. public:  
  12.     PathHelper(){}  
  13.     ~PathHelper(){}  
  14.       
  15.     //得到上一級目錄  
  16.     static CString GetUpFolder(CString strPath)  
  17.     {  
  18.         int nPos = strPath.ReverseFind('\\');  
  19.         return strPath.Left(nPos);  
  20.     }  
  21.           
  22.     //獲取運行目錄(exe所在目錄)  
  23.     static CString GetModuleFolder( HMODULE hModule )  
  24.     {  
  25.         TCHAR   szPath[MAX_PATH] = {0} ;  
  26.         GetModuleFileName( hModule, szPath, MAX_PATH ) ;  
  27.         ZeroMemory(_tcsrchr(szPath,_T('\\')), _tcslen(_tcsrchr(szPath,_T('\\') ) )*sizeof(TCHAR)) ;  
  28.         return CString(szPath) ;  
  29.     }  
  30.   
  31.     //獲取運行模塊文件全路徑  
  32.     static CString GetModuleFilePath( HMODULE hModule )  
  33.     {  
  34.         TCHAR   szPath[MAX_PATH] = {0} ;  
  35.         GetModuleFileName( hModule, szPath, MAX_PATH ) ;  
  36.         return CString(szPath) ;  
  37.     }  
  38.   
  39.     static CString GetSysFolder ()  
  40.     {  
  41.         TCHAR szPath[100] ={0};  
  42.         GetSystemDirectory ( szPath, 100 ) ;  
  43.         return CString(szPath) ;  
  44.     }  
  45.   
  46.     static CString GetWindowsFolder()  
  47.     {  
  48.         TCHAR szPath[100] ={0};  
  49.         GetWindowsDirectory ( szPath, 100 ) ;  
  50.         return CString(szPath) ;  
  51.     }  
  52.   
  53. };  
  54.   
  55.   
  56. #endif  


 

第二部分 系統特殊路徑

 

使用API: 

BOOL SHGetSpecialFolderPath(
	HWND hwndOwner,
	LPTSTR lpszPath,
	int nFolder,
	BOOL fCreate
);

 

參數說明:

 

HWND hwndOwner:如果在一個對話框或messagebox上顯示時,用到的窗口句柄。這里我們單純獲取路徑,一般設NULL。

 

LPTSTR lpszPath : 接收路徑的字符串

int nFolder : 微軟定義好的一個標志,用到代表要獲取那種目錄。

常見的如下:

 

[cpp]  view plain copy
  1. CSIDL_BITBUCKET   回收站       
  2. CSIDL_CONTROLS   控制面板       
  3. CSIDL_DESKTOP   Windows   桌面Desktop       
  4. CSIDL_DESKTOPDIRECTORY   Desktop的目錄       
  5. CSIDL_DRIVES   我的電腦       
  6. CSIDL_FONTS   字體目錄       
  7. CSIDL_NETHOOD   網上鄰居       
  8. CSIDL_NETWORK   網上鄰居虛擬目錄       
  9. CSIDL_PERSONAL   我的文檔       
  10. CSIDL_PRINTERS   打印機       
  11. CSIDL_PROGRAMS   程序組       
  12. CSIDL_RECENT   最近打開的文檔       
  13. CSIDL_SENDTO   “發送到”菜單項       
  14. CSIDL_STARTMENU   任務條啟動菜單項       
  15. CSIDL_STARTUP   啟動目錄       
  16. CSIDL_TEMPLATES   文檔模板        

具體內容可以參照 shlobj.h 。

 

BOOL fCreate : 如果該文件夾不存在,是否創建它。 一般我們不改系統文件夾,就設FASLE。

 

調用示例:

 

[cpp]  view plain copy
  1.        CHAR szPath[MAX_PATH] = {0};   
  2. SHGetSpecialFolderPath(NULL, szPath,CSIDL_DESKTOP, FALSE);    
  3. MessageBox(szPath);   
效果:

 

 

原文地址:http://blog.csdn.net/dpsying/article/details/17534661


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM