zoukankan      html  css  js  c++  java
  • (转)c++--【路径相关方法】1,获取各种系统路径

     

    1,目的

    介绍获取Windows常用的一些系统路径的方法(当前用户),如:桌面、我的文档等。

    并提供简单封装为类PathHelper,供以后工程快捷调用。

    2,原理&实现

     

    第一部分 常用系统及进程目录

     

    ①获取系统system32路径:(c:windowssystem32)

     使用函数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

  • 相关阅读:
    每日总结2021.9.14
    jar包下载mvn
    每日总结EL表达语言 JSTL标签
    每日学习总结之数据中台概述
    Server Tomcat v9.0 Server at localhost failed to start
    Server Tomcat v9.0 Server at localhost failed to start(2)
    链表 java
    MVC 中用JS跳转窗体Window.Location.href
    Oracle 关键字
    MVC 配置路由 反复走控制其中的action (int?)
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/7388440.html
Copyright © 2011-2022 走看看