牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Visual C++获取程序当前路径

来源:http://www.ieee.org.cn/dispbbs.asp?boardID=61&ID=33357


1.

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



2.

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

posted on 2008-06-19 15:15 杨粼波 阅读(2178) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理