~$ 存档

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

获取进程的绝对路径(代码同时操作字符串获取了文件目录):

第一种代码:

wstring GetProgramDir()    
{     
    TCHAR exeFullPath[MAX_PATH]; // Full path  
    GetModuleFileName(NULL,exeFullPath,MAX_PATH);  
    wstring strPath = __TEXT("");         
    strPath = (wstring)exeFullPath;    // Get full path of the file  
    int pos = strPath.find_last_of(L'\\', strPath.length());  
    return strPath.substr(0, pos);  // Return the directory without the file name  
}     

第二种代码:

CString sPath;
GetModuleFileNameA(NULL, sPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
sPath.ReleaseBuffer();
int nPos=sPath.ReverseFind(_T('\\'));
sPath = sPath.Left(nPos);

 

posted on 2017-07-11 11:26  LuoTian  阅读(1122)  评论(0编辑  收藏  举报