栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++获取工程路径、解决方案路径和.exe路径

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++获取工程路径、解决方案路径和.exe路径

c++系列文章目录

文章目录
  • c++系列文章目录
  • 前言
  • 一、获取工程路径
  • 二、获取可执行文件路径

前言

在编程中经常会用到文件路径,如果写绝对路径,把工程复制到另一台机器上,在别人机器上路径就错误了,所以最好用相对路径,这样工程拷贝到其他机器上也不用修改文件路径了。

一、获取工程路径

得到xxxxx.vcxproj 目录

#include 
#include 
#include 

string getFilePath()
{
	char* path = nullptr;
	path = _getcwd(nullptr, 1);
	puts(path);

	string filePath(path);
	return filePath;

	delete path;
	path = nullptr;

}

二、获取可执行文件路径

得到xxx.exe路径,注意GetModuleFileName会得到路径+“xxxx.exe”

string TCHAT_to_string(TCHAR* STR)
{
	int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, nullptr, 0, nullptr, nullptr);
	char* chRtn = new char[iLen * sizeof(char)];
	WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, nullptr, nullptr);
	string str(chRtn);

	int pos = str.find_last_of("\", str.length());
	string exePath = str.substr(0, pos);

	return exePath;
}

string getExePath()
{
	TCHAR szExePath[MAX_PATH];
	GetModuleFileName(nullptr, szExePath, sizeof(szExePath));

	string exePath = TCHAT_to_string(szExePath);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004951.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号