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