分享

取当前用户的AppData路径

 QomoIT 2018-05-11
  1. // TestSHGetSpecialFolderLocation.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. #include <io.h>  
  7.   
  8.   
  9. #include <Shlobj.h>  
  10. #pragma comment( lib, "shell32.lib"  
  11.   
  12. #include <string>  
  13. #include <iostream>  
  14. )  
  15.   
  16. using namespace std;  
  17.   
  18. /* 
  19. 功能:取用户的AppData路径,并在下面建一个新的目录 
  20. 测试环境:WinXP SP3 + VS2010 
  21. 最后更新日期:2014-6-12      
  22. 参考资料:http://blog.csdn.net/roger_77/article/details/1538447 
  23. */  
  24.   
  25. std::wstring GetLocalAppDataPath()  
  26. {  
  27.     wchar_t m_lpszDefaultDir[MAX_PATH];  
  28.     wchar_t szDocument[MAX_PATH] = {0};     
  29.     memset(m_lpszDefaultDir, 0, _MAX_PATH);  
  30.   
  31.     LPITEMIDLIST pidl = NULL;  
  32.     SHGetSpecialFolderLocation(NULL,   CSIDL_LOCAL_APPDATA,   &pidl);  
  33.     if   (pidl && SHGetPathFromIDList(pidl, szDocument))  
  34.     {     
  35.         GetShortPathName(szDocument,m_lpszDefaultDir, _MAX_PATH);  
  36.     }  
  37.   
  38.     std::wstring wsR = m_lpszDefaultDir;  
  39.   
  40.     return wsR;  
  41. }  
  42.   
  43. std::wstring GetLocalAppDataPath(std::wstring appName)  
  44. {  
  45.     std::wstring path =  GetLocalAppDataPath();  
  46.     path.append(L"\\");  
  47.     path.append(appName);  
  48.   
  49.     if ( _waccess(path.c_str(), 0) == -1 )  
  50.     {  
  51.         _wmkdir(path.c_str());  
  52.     }  
  53.   
  54.     return path;  
  55. }  
  56.   
  57. int _tmain(int argc, _TCHAR* argv[])  
  58. {  
  59.     const std::wstring AppName = L"kagula";  
  60.   
  61.     std::wstring path =  GetLocalAppDataPath(AppName);  
  62.     std::wcout << path << std::endl;  
  63.   
  64. #ifdef _DEBUG  
  65.     ::system("pause");  
  66. #endif  
  67.   
  68.     return 0;  
  69. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多