Windows环境下的C++获取当前程序的exe文件路径
来源:互联网 发布:兵器科学与技术 知乎 编辑:程序博客网 时间:2024/05/16 10:05
获取.exe文件的路径
多字节集环境下
1. #include "stdafx.h"
2. #include <Windows.h>
3. #include <iostream>
4. #include <string>
5. using namespace std;
6.
7. string GetProgramDir()
8. {
9. char exeFullPath[MAX_PATH]; // Full path
10. string strPath = "";
11.
12. GetModuleFileName(NULL,exeFullPath,MAX_PATH);
13. strPath=(string)exeFullPath; // Get full path of the file
14.
15. int pos = strPath.find_last_of('\\', strPath.length());
16. return strPath.substr(0, pos); // Return the directory without the file name
17. }
18.
19. int _tmain(int argc, _TCHAR* argv[])
20.{
21. string strProgramDir = GetProgramDir();
22. cout<<strProgramDir<<endl;
23.
24. return 0;
25. }
Unicode字符集环境下
1. #include "stdafx.h"
2. #include <Windows.h>
3. #include <iostream>
4. #include <string>
5. using namespace std;
6.
7. string GetProgramDir()
8. {
9. wchar_t exeFullPath[MAX_PATH]; // Full path
10. string strPath = "";
11.
12. GetModuleFileName(NULL,exeFullPath,MAX_PATH);
13. char CharString[MAX_PATH];
14. size_t convertedChars = 0;
15. wcstombs_s(&convertedChars, CharString, MAX_PATH, exeFullPath , _TRUNCATE);
16.
17. strPath=(string)CharString; // Get full path of the file
18.
19. int pos = strPath.find_last_of('\\', strPath.length());
20. return strPath.substr(0, pos); // Return the directory without the file name
21. }
22.
23. int _tmain(int argc, _TCHAR* argv[])
24.{
25. string strProgramDir = GetProgramDir();
26. cout<<strProgramDir<<endl;
27.
28. return 0;
29. }
- Windows环境下的C++获取当前程序的exe文件路径
- C/C++获取相对当前启动路径(.exe)下的文件全路径名
- c/c++ 获取当前程序(EXE)所在的路径
- c/c++ 获取当前程序(EXE)所在的路径
- c++_获取当前程序(EXE)所在的路径
- c/c++ 获取当前程序(EXE)所在的路径
- C获取当前程序路径, exe路径
- Windows环境C/C++ 获取当前执行程序路径的两个函数
- windows下c语言获取程序当前的执行目录,读文件的代码片
- C++获取当前可执行程序(*.exe)所在的路径
- C++获取当前可执行程序(*.exe)所在的路径
- 获取当前运行(。。。。.exe)程序的路径
- C++获取当前可执行程序(*.exe)所在的路径
- c# winform获取当前路径exe程序的版本号
- 如何获取当前程序文件的路径
- VC获取当前程序文件的路径
- 如何获取当前程序文件的路径
- 获取当前工程的.exe文件的路径和目录
- JavaWeb--基础进阶《二》:Java基础-变量与数据类型
- 华为麦芒 6 上手评测:能否继续麦芒 5 的辉煌?
- 翻译 | 关键CSS和Webpack: 减少阻塞渲染的CSS的自动化解决方案
- QNX配置网卡
- 汉诺塔问题分析
- Windows环境下的C++获取当前程序的exe文件路径
- 消除手机上APP启动时的白屏现象
- 12.IO类(C++)
- sql语句中GROUP BY 和 HAVING的使用 count()
- Spring框架中ModelAndView用法分享
- Java-Regular Expression Basic
- linux用户和用户组管理(添加、删除、修改)及说明
- 处理undo回滚段问题
- 2017-10-10离线赛