使用argv[0]获取当前路径:
int _tmain(int argc, _TCHAR* argv[])
{
string strAppPath(argv[0]);
return 0;
}
使用时报错:IntelliSense: 没有与参数列表匹配的构造函数 "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [其中 _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" 实例 参数类型为: (_TCHAR *)
原因为_TCHAR*类型定义为宽字符:
typedef wchar_t _TCHAR;
将字符集设置为使用多字节字符集即可,如图:
获取路径方法:
int iPos1 = strAppPath.find_last_of('\\');
string basePath(strAppPath.substr(0, iPos1 + 1));