在linux和windows下获取路径的常用方法

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);

  }

}

 

备注:上述提供获取路径的常用方法,从而为配置其它文件路径(如:配置文件路径)提供参考。

在获取上述路径后,在此基础上,进而去设置配置文件路径。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值