MFC 将 '当前工作路径' 改为 'exe所在路径'(转)
原文转自 https://blog.csdn.net/morewindows/article/details/8683519
调整进程当前目录为程序可执行文件所在目录是个非常实用的方法。为了更加的让代码复用,本文将调整进程当前目录为程序可执行文件所在目录这一功能封装为三个实用函数——
1.SplitPathFileName
这个函数将文件全名(带路径)分解成路径名,文件名,后缀名。
2.GetProcessPathNameAndFileName
得到当前进程可执行文件的路径名,文件名,后缀名。
3.AdjustProcessCurrentDirectory
调整进程当前目录为程序可执行文件所在目录
各函数使用示范可以参见如下程序:
//调整进程当前目录为程序可执行文件所在目录
//http://blog.csdn.net/morewindows/article/details/8683519
//By MoreWindows( http://blog.csdn.net/MoreWindows )
#include <windows.h>
#include <stdio.h>
#include <conio.h> //将文件全名(带路径)分解成路径名,文件名,后缀名
//C:\test\test.exe -> "C:\test\", "test", ".exe"
//By MoreWindows( http://blog.csdn.net/MoreWindows )
void SplitPathFileName(const char *pstrPathFileName, char *pstrPath, char *pstrFileName, char *pstrExtName)
{
if (pstrPath != NULL)
{
char szTemp[MAX_PATH];
_splitpath(pstrPathFileName, pstrPath, szTemp, pstrFileName, pstrExtName);
strcat(pstrPath, szTemp);
}
else
{
_splitpath(pstrPathFileName, NULL, NULL, pstrFileName, pstrExtName);
}
} //得到当前进程可执行文件的路径名,文件名,后缀名
//如运行C:\test\test.exe 得到 "C:\test\", "test", ".exe"
//By MoreWindows( http://blog.csdn.net/MoreWindows )
BOOL GetProcessPathNameAndFileName(char *pstrPath, char *pstrFileName, char *pstrExtName)
{
char szExeFilePathFileName[MAX_PATH];
if (GetModuleFileName(NULL, szExeFilePathFileName, MAX_PATH) == )
return FALSE; SplitPathFileName(szExeFilePathFileName, pstrPath, pstrFileName,pstrExtName);
return TRUE;
} //调整进程当前目录为程序可执行文件所在目录
//By MoreWindows( http://blog.csdn.net/MoreWindows )
BOOL AdjustProcessCurrentDirectory()
{
char szPathName[MAX_PATH];
GetProcessPathNameAndFileName(szPathName, NULL, NULL);
return SetCurrentDirectory(szPathName);
} int main()
{
printf(" 调整进程当前目录为程序可执行文件所在目录 \n");
printf(" - http://blog.csdn.net/morewindows/article/details/8683519 -\n");
printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n"); char szCurrentDirectory[MAX_PATH];
GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
printf("进程当前目录为: \n%s\n", szCurrentDirectory); AdjustProcessCurrentDirectory(); GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
printf("\n调整后,进程当前目录为: \n%s\n", szCurrentDirectory);
getch();
return ;
}
通过CMD来调用这个程序看看。

由图可以看出,程序的当前目录已经被调整到程序可执行文件所在目录了。
附1:得到程序所在目录还可以使用PathRemoveFileSpec函数。20130507
本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165721
欢迎关注微博:http://weibo.com/MoreWindows
MFC 将 '当前工作路径' 改为 'exe所在路径'(转)的更多相关文章
- exe所在路径
procedure TForm1.btn1Click(Sender: TObject); begin //完整路径 E:\UsrSoft\Desktop\FileNameDemo\Project1.e ...
- Qt5_当前exe所在路径
可以通过以下方式来获取: 1. #include <QDir>#include <QDebug> QDir dir; qDebug() << "curre ...
- C# 获取当前执行DLL 所在路径
有的时候,当前执行的DLL 和启动的EXE 所在路径并不一致,这时我们想要获得当前执行DLL 所在路径可以使用下面的方法. // Summary: // Gets the path or UNC lo ...
- Visual Studio 2017 调试器的工作进程(msvsmon.exe)意外退出 调试将终止
开发环境: Windows 10 in Parallels Desktop Visual Studio 15.6 场景还原: 使用 Visual Studio 15.6 (即 Visual Studi ...
- 关于富文本编辑器—UEditor(java版)的使用,以及如何将UEditor的文件/图片上传路径改成绝对路径
突然发现好久没写博客了,感觉变懒了,是要让自己养成经常写文章的习惯才行.既可以分享自己的所学,和所想,和大家一起讨论,发现自己的不足的问题. 大家可能经常会用到富文本编辑器,今天我要说的是UEdito ...
- 织梦dedecms5.7二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法
dedecms升级到5.7SP1后,开启二级域名,你会发现,在二级域名下的文章,上传的图片地址都是:二级域名+图片地址,这是相对路径.但是你的图片是默认上传在uploads里面的.应该使用绝对路径.这 ...
- 中文Ubuntu里用户目录里的路径改成英文
(附注:转载于http://www.linuxdiyf.com/linux/201105/56.html) 为了使用起来方便,装了Ubuntu中文版,自然在home文件里用户目录的"桌面&q ...
- 得到本地应用程序的EXE的路径
实现效果: 知识运用: Application类的ExecutablePath属性 //获取启动了应用程序的可执行文件的路径和可执行文件的名称 public static string Exec ...
- dedecms5.7二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法
dedecms5.7(织梦CMS5.7)二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法 dedecms升级到5.7SP1后,开启二级域名,你会发现,在二级域名下的文章,上传的图片地址都是: ...
随机推荐
- BurpSuite 的使用
最好用的抓包软件, 不只是抓包软件 IE/Chrome中设置代理的方法是, 打开Internet选项面板->连接->局域网设置->取消勾选的使用自动配置脚本->勾选为LAN使用 ...
- 课后题2.87&2.86
课后题2.86&2.87 单纯就是想加点分第十章的题目都被做过了就做下第二章的,正好复习一下前面学的知识,第二章给我剩下的题目也不多了,我就挑了这个题目. 2.86 考虑一个基于IEEE浮点格 ...
- 剑指offer题目练习一
看见了一道二维数组找数的题,已排好序的数组(从左至右从上到下,都是由小变大的)让找数,瞬间就出思路了,并没有必要去看他的解释,两次二分就搞定了. #include<cstdio> #inc ...
- 编译ffmpeg
1.下载ffmpeg-0.9.2.tar.gz http://ffmpeg.org/releases/ 下载需要的版本 2.编译linux (centos5.5) 64位版本 ./configure ...
- eclipse、myeclipse 反编译插件 轻松查看源代码
java反编译插件:Eclipse Class Decompiler,能够很方便的使用本插件查看类库源码,以及采用本插件进行Debug调试. eclipse中安装Eclipse Class Decom ...
- Robocopy.exe使用详例
Robocopy.exe使用详例 Robocopy.exe 是 微软在Windows server 2003 Resource Kit Tools 里面提供的程序来做备份的.Vis ...
- centso下如何解压RAR文件
tar -xvf rarlinux-3.9.3.tar.gz cd rar make 看见下面这些信息就是安装成功了 mkdir -p /usr/local/bin mkdir -p /usr/l ...
- 后端接口迁移(从 webapi 到 openapi)前端经验总结
此文已由作者张磊授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前情提要 以前用的是 webapi 现在统一切成 openapi,字段结构统统都变了 接入接口 20+,涉及模 ...
- 【vim环境配置】详细实录
[写在前面] 以下的所有内容主要参照: https://github.com/yangyangwithgnu/use_vim_as_ide . 原blog作者写的非常用心,建议大家都去看看.(个人觉得 ...
- 【Substring with Concatenation of All Words】cpp
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...