Windows VC++ 调整进程当前目录为程序可执行文件所在目录
来源:互联网 发布:淘宝店铺广告图片750 编辑:程序博客网 时间:2024/05/28 15:16
本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165721
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8683519
欢迎关注微博:http://weibo.com/MoreWindows
调整进程当前目录为程序可执行文件所在目录是个非常实用的方法。为了更加的让代码复用,本文将调整进程当前目录为程序可执行文件所在目录这一功能封装为三个实用函数——
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) == 0)
- 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 0;
- }
通过CMD来调用这个程序看看。
由图可以看出,程序的当前目录已经被调整到程序可执行文件所在目录了。
附1:得到程序所在目录还可以使用PathRemoveFileSpec函数。20130507
本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165721
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8683519
0 0
- Windows VC++ 调整进程当前目录为程序可执行文件所在目录
- Windows VC++ 调整进程当前目录为程序可执行文件所在目录
- GetModuleFileName 获取当前程序可执行文件所在目录
- linux 获取当前进程的可执行文件所在的目录
- 获得当前进程的可执行程序所在的目录
- VC获取程序所在目录
- 获取当前程序所在路径/目录
- 中获取当前程序本身所在目录
- MFC 获得当前程序所在目录
- 【Go】获取当前程序EXE所在目录
- VC 取得程序所在目录的方法
- 如何获得当前本进程所在的目录
- 如何获取当前进程所在目录的方法
- shell 里面求出当前运行进程的所在目录
- 获取当前进程文件所在目录,用于替代GetCurrentDirectory
- 获取.EXE可执行文件所在的目录路径
- C#如何获得当前程序所在的目录
- java中取得 当前程序运行所在的目录
- 第7天-2 flashback table 】【flashback version query 】【flashback transaction query 】【flashback database】
- 增加NOT的逻辑条件处理
- 第7天 asm安装】
- 基础概念:从概念及汇编角度解释指针本质
- .Net中DataAdapter批量插入和更新数据总结
- Windows VC++ 调整进程当前目录为程序可执行文件所在目录
- 完成端口总结(二)
- 妙味云课堂_HTMLCSS基础_代码初识-1
- VC++得到系统特殊文件夹路径
- POJ1830(异或方程组的高斯消元)
- 获取文件图标,类型名称,属性 SHGetFileInfo
- 2013.12.16
- 不知道是不是RakNet的BUG
- LINUX ls2 further