C/MFC如何获得应用程序当前路径(整理)
2012-08-23 14:15
399 查看
第一种方法:
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分析文件名得到路径
例如:
//得到当前路径
/*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;
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);
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分析文件名得到路径
例如:
//得到当前路径
/*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;
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);
相关文章推荐
- C/MFC如何获得应用程序当前路径(整理)
- C/MFC如何获得应用程序当前路径(整理)
- C/MFC如何获得应用程序当前路径(整理)
- C/MFC如何获得应用程序当前路径(整理)
- C/MFC如何获得应用程序当前路径(整理)
- 如何通过.Net Compact Framework来获得应用程序的当前路径
- 如何获得当前应用程序所在路径
- C++/VC/MFC获得应用程序的当前路径
- 在jar包中如何获得当前项目的绝对路径
- MFC 获得当前项目的.exe文件路径
- VC 获得当前应用程序所在路径(exe文件的路径)
- C#编程:如何获得当前进程运行路径
- MFC获得当前运行程序的路径
- MFC 获得当前程序目录 当前绝对路径
- 请问如何在WINCE的C#开发中获得当前路径
- 在jar包中如何获得当前项目的绝对路径
- C# 如何获取当前应用程序的上一级路径
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- 如何得到应用程序的当前路径
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数