设置当前exe执行文件为进程工作目录
2015-07-13 16:58
302 查看
设置当前exe执行文件为进程工作目录
两种办法:
1, API
这个函数将文件全名(带路径)分解成路径名,文件名,后缀名。
2, API
使用例子:
两种办法:
1, API
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );
这个函数将文件全名(带路径)分解成路径名,文件名,后缀名。
2, API
BOOL PathRemoveFileSpec( LPTSTR pszPath );
使用例子:
#include <windows.h> #include <iostream> #include <Shlwapi.h> //PathRemoveFileSpec函数头文件 #pragma comment(lib, "shlwapi.lib") //VS2013下需加才可以使用PathRemoveFileSpec using namespace std; int main() { char szCurrentDirectory[MAX_PATH], szExeFilePathFileName[MAX_PATH]; //获取当前进程工作目录 GetCurrentDirectory(MAX_PATH, szCurrentDirectory); //szCurrentDirectory == 输出 E:\Projects\1 cout << "进程当前工作目录为: " << szCurrentDirectory <<endl; //szExeFilePathFileName == exe的路径是 E:\Projects\1\Debug\11.exe GetModuleFileName(NULL, szExeFilePathFileName, MAX_PATH); char drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH]; //szExeFilePathFileName == exe的路径 E:\Projects\1\Debug\11.exe //drive == E: 盘符 //dir == \Projects\1\Debug\ 文件中间的路径 //fname == 11 不带拓展名的文件名 //ext == .exe 文件拓展名 //第一种办法获得exe文件目录 _splitpath(szExeFilePathFileName, drive, dir, fname, ext); //把盘符和文件中间路径组合起来E:\Projects\1\Debug\ strcat(drive, dir); GetModuleFileName(NULL, szExeFilePathFileName, MAX_PATH); //第二种办法获得exe文件目录 //szExeFilePathFileName == E:\Projects\1\Debug\11.exe PathRemoveFileSpec(szExeFilePathFileName); //设置进程工作目录szExeFilePathFileName == E:\Projects\1\Debug SetCurrentDirectory(szExeFilePathFileName); //获得进程工作目录 GetCurrentDirectory(MAX_PATH, szCurrentDirectory); //szCurrentDirectory == E:\Projects\1\Debug cout << "调整后, 进程的工作目录为: " << szCurrentDirectory << endl; system("pause"); return 0; }
相关文章推荐
- jquery easyui datagrid 设置设置在选中
- shell脚本使用记录
- 剑指offer:二维数组的查找
- OC 基础之----- Block和语法糖
- oracle中怎样获取当前月上个月的第一天和最后一天?
- android studio中出现的问题
- 关于IE7 z-index的浏览器兼容性问题完美解决方案
- iOS实现UIImageView透明区域点击事件穿透
- 表达式求值难度4
- Spring @Transactional (一)
- Android EditText输入最大值提示功能
- 启动erlang/OTP里面的Web服务器(补充)
- Ruby on Rails Tutorial 第六章 用户模型
- android知识杂记(一)
- SQL Sever——远程过程调用失败(0x800706be)
- OC 基础之----NSSet
- IllegalArgumentException: MALFORMED
- 表达式求值难度3
- 【Android】设置全屏和横屏
- awk 应用