1.在windows下获取执行文件路径:
string GetExeFilePath()
{
char szPath[512] = {0};
DWORD nSize = sizeof(szPath);
DWORD nLen = ::GetModuleFileName(NULL, szPath, nSize);//获取可执行文件路径,包含可执行文件名,如:D:\test\demo.exe
if (0 == nLen)
{
throw;
}
szPath[nLen] = 0;
string strPath = szPath;
return strPath;
}
2.在linux下获取主目录路径:
char* GetExeFilePath()
{
char *lpszPaht = getenv("HOME");// 获取主目录路径,如:/home/usrname
if (NULL != lpszValue)
{
printf("get home path default\n");
}
return lpszPath;
}
3.在linux下获取执行路径的方法
char* GetExeFilePath()
{
char sysfile[25] = "/proc/self/exe";
char selffile[256]={0};
if(-1 != readlink(sysfile,selffile,256))
{
printf("exe file path=%s\n",selffile);
}
}
备注:上述提供获取路径的常用方法,从而为配置其它文件路径(如:配置文件路径)提供参考。
在获取上述路径后,在此基础上,进而去设置配置文件路径。