C MFC如何獲得應用程式當前路徑(整理)

2021-07-12 04:25:26 字數 1892 閱讀 5084

標籤: path

buffer

null

exeext

2012-08-23 14:15

6592人閱讀收藏

舉報

第一種方法:

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;

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;

intposition=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

char buf[_max_path];

_getcwd(buf,_max_path);

第四種是得到作業系統所在的目錄

char buf[100];

getsystemdirectory(buf,100);

messagebox(buf);

頂 0 踩

0

C MFC如何獲得應用程式當前路徑(整理)

第一種方法 dword getcurrentdirectory dword nbufferlength,size,in characters,of directory buffer lptstr lpbuffer pointer to buffer for current directory boo...

winfom獲取應用程式當前路徑

system.diagnostics process getcurrentprocess mainmodule filename 獲取新的 process 元件並將其與當前活動的程序關聯的主模組的完整路徑,包含檔名 程序名 返回 e c 學習 獲取當前路徑 獲取當前路徑 bin debug 獲取當前...

Winform獲取應用程式的當前路徑方法集合

winform獲取應用程式的當前路徑的方法集合,具體如下,值得收藏 3 獲取當前程序的完整路徑,包含檔名 程序名 4 string str this.gettype assembly.location 5 result x exe exe檔案所在的目錄 exe檔名 6 7 獲取新的process 元...