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