一个欲儿的博客

一个欲儿的博客

C++ 获取程序运行绝对路径
2019-08-15


#include <windows.h>
#include <string>
#include <string.h>
#include <iostream>
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 path = GetProgramDir();
	cout << path << endl;
	while (1);
	return 0;
}


发表评论: