1. 程式人生 > >VS2010/MFC 獲取當前程式路徑 CString型別

VS2010/MFC 獲取當前程式路徑 CString型別

第一種方案:
TCHAR szFilePath[MAX_PATH + 1]={0};
GetModuleFileName(NULL, szFilePath, MAX_PATH);                            // C:\**\**\Test.exe
(_tcsrchr(szFilePath, _T('\\')))[1] = 0; // 刪除檔名,只獲得路徑字串   // C:\**\**\
CString str_url = szFilePath;                                                                        // C:\**\**\

CString strBookPath = str_url+ L"test.xls";

第二種方案:
CString GetAppPath()
{
char szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
CString strPath = szPath;
int nPos = strPath.ReverseFind('\\');
int nLen = strPath.GetLength();
if(nPos > 0)
strPath = strPath.Left(nPos);
return strPath;
}