MFC获取exe文件所在的文件夹

本示例展示的是利用函数cstrGetExePath(),返回当前的exe程序所在的文件夹路径。即使路径里有中文名也适用。

本示例受了 https://blog.csdn.net/jaken99/article/details/78231872的启发

代码:

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <afx.h>
#include <Windows.h>
#include <string.h>


CString cstrGetExePath(void)
{
	CString cstrPath;
	char arrFilePath[1024] = {0};
	//HMODULE hModuleInst = _AtlBaseModule.GetModuleInstance();  
	GetModuleFileNameA(NULL, arrFilePath, 1024);

	char * pPos = NULL;
	pPos = strrchr(arrFilePath, '\\');
	if(pPos)
		*pPos = NULL;

	if(arrFilePath[strlen(arrFilePath) - 1] != '\\')
		strcat_s(arrFilePath, "\\");
	cstrPath.Format("%s", arrFilePath);
	return cstrPath;
}

int _tmain(int argc, _TCHAR* argv[])
{
	CString cstrPath = cstrGetExePath();
	return 0;
}

为了能显示汉字,请采用“多字节字符集”

设置MTd模式:

效果:

猜你喜欢

转载自blog.csdn.net/liji_digital/article/details/84197529