用GetModuleFileName(NULL,exeFullPath,MAX_PATH)得到当前执行文件的全路径。
#include <Windows.h> #include <iostream> #include <string> using namespace std; string GetProgramDir() { char exeFullPath[MAX_PATH]; // Full path string strPath = ""; GetModuleFileName(NULL,exeFullPath,MAX_PATH); strPath=(string)exeFullPath; // Get full path of the file int pos = strPath.find_last_of('\\', strPath.length()); return strPath.substr(0, pos); // Return the directory without the file name } int main () { string str = ""; str = GetProgramDir(); cout << str << endl; return 0; }