首页 C/C++修行正文

C++ 获取程序运行绝对路径

欲儿 C/C++修行 2019-08-15 571 0


#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;
}


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论