获取程序数据路径(转)

 

GetCurrWorkingDir(){
     CString strPath;
     GetCurrentDirectory(MAX_PATH,strPath.GetBuffer(MAX_PATH));
     strPath.ReleaseBuffer();
     
return strPath;
}

 

CString CPropertySampleApp::GetCurrWorkingDir()
{
  CString strPath;
  TCHAR szFull[_MAX_PATH];
  TCHAR szDrive[_MAX_DRIVE];
  TCHAR szDir[_MAX_DIR];
  ::GetModuleFileName(NULL, szFull, 
sizeof(szFull)/sizeof(TCHAR));
  _tsplitpath(szFull, szDrive, szDir, NULL, NULL);
  _tcscpy(szFull, szDrive);
  _tcscat(szFull, szDir);
  strPath 
= CString(szFull);
  
return strPath;
}

  使用第一段代码可以获得应用程序运行时所在目录。但由于在使用Microsoft Visual Studio.NET编译并运行项目时,真正的宿主是IDE,所以当前目录是项目所在目录,并不是DEBUG或者RELEASE目录,这一点需要注意。而第二段代码可以动态解决此问题。并且增加了Unicode支持(TCHAR)。


原文地址:https://www.cnblogs.com/qq78292959/p/2076604.html