MFC中获取当前路径

来源:互联网 发布:linux查询cpu和内存 编辑:程序博客网 时间:2024/05/17 09:28
  1. 1. 获取Debug或Release所在的路径  
  2. CString GetModuleDir()   
  3. {   
  4.  HMODULE module = GetModuleHandle(0);   
  5.  char pFileName[MAX_PATH];   
  6.  GetModuleFileName(module, pFileName, MAX_PATH);   
  7.    
  8.  CString csFullPath(pFileName);   
  9.  int nPos = csFullPath.ReverseFind( _T('\\') );   
  10.  if( nPos < 0 )   
  11.   return CString("");   
  12.  else   
  13.   return csFullPath.Left( nPos );   
  14. }  
  15.   
  16.    
  17.   
  18. 2. 获取当前工作路径(dsp所在路径)  
  19.   
  20. //获取工作路径  
  21. CString GetWorkDir()   
  22. {    
  23.  char pFileName[MAX_PATH];   
  24.  int nPos = GetCurrentDirectory( MAX_PATH, pFileName);   
  25.    
  26.  CString csFullPath(pFileName);    
  27.  if( nPos < 0 )   
  28.   return CString("");   
  29.  else   
  30.   return csFullPath;   
  31. }  


路径分解函数:

[cpp] view plain copy
  1. char   a_sFileName[256];     
  2.     GetModuleFileName(NULL,a_sFileName,256);   
  3. CString sPath;  
  4.   
  5.     CString sDrive;//磁盘名  
  6.     CString sDir;//文件路径  
  7.     CString sFileName;//取出文件路径后的文件名  
  8.     CString sExt;//文件扩展名  
  9.   
  10.     char drive[_MAX_DRIVE];//磁盘名  
  11.     char dir[_MAX_DIR];//路径名  
  12.     char fname[_MAX_FNAME];//文件名  
  13.     char ext[_MAX_EXT];//扩展名  
  14.   
  15.     _splitpath(a_sFileName, drive, dir, fname, ext );  
  16.   
  17.     sDrive.Format("%s",drive);  
  18.     sDir.Format("%s",dir);  
  19.     sFileName.Format("%s",fname);  
  20.     sExt.Format("%s",ext);  
  21.   
  22.     sPath= sDrive + sDir + sFileName + a_sSuffix + sExt;  
0 0
原创粉丝点击