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",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.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...