C/MFC如何获得应用程序当前路径(整理)

第一种方法:
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);

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值