(原创)VC++ MFC获取程序当前运行目录
就是当前EXE运行的目录
CSTRING版
CString GetModuleDir()
{
HMODULE module = GetModuleHandle(0);
char pFileName[MAX_PATH];
GetModuleFileNameA(module, pFileName, MAX_PATH);
CString csFullPath(pFileName);
int nPos = csFullPath.ReverseFind( _T('\\') );
if( nPos < 0 )
return CString("");
else
return csFullPath.Left( nPos );
}
String版
std::string GetModuleDir()
{
HMODULE module = GetModuleHandle(0);
char pFileName[MAX_PATH];
GetModuleFileNameA(module, pFileName, MAX_PATH);
string strFullPath(pFileName);
string::size_type nPos=strFullPath.find_last_of("\\");
string strfolderPath=strFullPath.substr(0,nPos);
return strfolderPath;
}
建议如果程序不需要MFC支持的话就用STRING版就可以了,生成的文件体积减少几倍