獲取當前程式執行目錄

2022-06-23 12:45:13 字數 991 閱讀 8088

1. 使用_getcwd()

std::string

getmodulepath()

;

char* path =nullptr;

path = (char*)malloc(ilength * sizeof(char

)); path =_getcwd(modulepath, ilength);

if (null !=path)}}

free

(path);

if (!flag)

mystr =modulepath;

return

mystr;

}

2. 使用getmodulefilename(),第乙個引數如果設定為null,返回的是開啟執行緒的exe,如果採用下面的做法,返回的是當前模組的路徑。

比如有乙個exe呼叫了乙個dll,exe在目錄e:\test\下,dll在e:\test\test\下,那麼引數設定為null,獲取到的路徑為e:\test\,如果採用**的寫法,獲取到的路徑是e:\test\test\

extern_c image_dos_header __imagebase;//

申明為全域性變數

std::

string

getmodulepath()

;

char path[1024] = ;

getmodulefilename((hinstance)&__imagebase, modulepath, 1024);//

return real lenghth

len =strlen(modulepath);

for (i = len - 1; i >= 0; i--)

}if (!flag)

std::

string

strpath(path);

return

strpath;

}

c winform 獲取當前程式執行根目錄

獲取程式的基目錄。獲取模組的完整路徑。system.diagnostics.process.getcurrentprocess mainmodule.filename 獲取和設定當前目錄 該程序從中啟動的目錄 的完全限定目錄。system.environment.currentdirectory 獲...

c winform 獲取當前程式執行根目錄

獲取程式的基目錄。獲取模組的完整路徑。string path system.diagnostics.process.getcurrentprocess mainmodule.filename 獲取和設定當前目錄 該程序從中啟動的目錄 的完全限定目錄。string path system.enviro...

c winform 獲取當前程式執行根目錄

1 獲取程式的基目錄。23 45 獲取模組的完整路徑。6system.diagnostics.process.getcurrentprocess mainmodule.filename78 9 獲取和設定當前目錄 該程序從中啟動的目錄 的完全限定目錄。10system.environment.cur...