第一種方法
DWORD GetCurrentDirectory( DWORD nBufferLength, // size, in characters, of directory buffer LPTSTR lpBuffer // pointer to buffer for current directory );
BOOL SetCurrentDirectory( LPCTSTR lpPathName // pointer to name of new current directory );
第二種方法
用GetModuleFileName得到應用程序的文件名(第一個參數為NULL)
再用_splitpath分析文件名得到路徑
再用_splitpath分析文件名得到路徑
例如:
//得到當前路徑 /*char buf[100]; GetCurrentDirectory(sizeof(buf),buf); MessageBox(buf); HINSTANCE hInst=NULL; hInst=AfxGetApp()->m_hInstance; char path_buffer[_MAX_PATH]; GetModuleFileName(hInst,path_buffer,sizeof(path_buffer));//得到exe文件的全路徑 //分離路徑和文件名。 char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( path_buffer, drive, dir, fname, ext ); CString Path; Path.Format("%s%s",drive,dir); char path[300]; strcpy(path,drive); strcat(path,dir);
又或:
TCHAR exeFullPath[MAX_PATH]; CString strPath; GetModuleFileName(NULL,exeFullPath,MAX_PATH); strPath=(CString)exeFullPath; int position=strPath.ReverseFind('\\'); strPath=strPath.Left(position+1); TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL,FilePath,MAX_PATH); (_tcsrchr(FilePath,'\\'))[1] = 0;//// 刪除文件名,只獲得路徑字串 // C:\**\**\ lstrcat(FilePath,_T("MY.ini"));
第三種方法
VC中__argv[0]可以得到exe的程序名,然后用_splitpath可以分解得到程序路徑。
第四種方法
#include<direct.h> char buf[_MAX_PATH]; _getcwd(buf,_MAX_PATH);
第五種方法
是得到操作系統所在的目錄
char buf[100]; GetSystemDirectory(buf,100); MessageBox(buf);