Windows VC++ 调整进程当前目录为程序可执行文件所在目录
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net 本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165721 转载请标明出处,原文地址:http://www.voidcn.com/article/p-zelavlra-yt.html 欢迎关注微博: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:testtest.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,pstrExtName);
}
}
//得到当前进程可执行文件的路径名,文件名,后缀名
//如运行C:testtest.exe 得到 "C:test",".exe"
//By MoreWindows( http://blog.csdn.net/MoreWindows )
BOOL GetProcessPathNameAndFileName(char *pstrPath,char *pstrExtName) {
char szExeFilePathFileName[MAX_PATH];
if (GetModuleFileName(NULL,szExeFilePathFileName,MAX_PATH) == 0)
return FALSE;
SplitPathFileName(szExeFilePathFileName,pstrExtName);
return TRUE;
}
//调整进程当前目录为程序可执行文件所在目录
//By MoreWindows( http://blog.csdn.net/MoreWindows )
BOOL AdjustProcessCurrentDirectory() {
char szPathName[MAX_PATH];
GetProcessPathNameAndFileName(szPathName,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 ) --nn");
char szCurrentDirectory[MAX_PATH];
GetCurrentDirectory(MAX_PATH,szCurrentDirectory);
printf("进程当前目录为: n%sn",szCurrentDirectory);
AdjustProcessCurrentDirectory();
GetCurrentDirectory(MAX_PATH,szCurrentDirectory);
printf("n调整后,进程当前目录为: n%sn",szCurrentDirectory);
getch();
return 0;
}
通过CMD来调用这个程序看看。 ? 由图可以看出,程序的当前目录已经被调整到程序可执行文件所在目录了。 ? 附1:得到程序所在目录还可以使用PathRemoveFileSpec函数。20130507 ? 本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165721 转载请标明出处,原文地址:http://www.voidcn.com/article/p-zelavlra-yt.html 欢迎关注微博:http://weibo.com/MoreWindows 再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 关闭最后一个后,在Windows 2012 Server核心上打开一个新的c
- 在Windows上识别未知进程的最佳方法是什么?
- 如何隐藏桌面的特定捷径
- windows – 在Emacs中重新排列文件名
- windows-server-2008 – Win7-Server2008 RDP连接挂起“保护
- 电子邮件 – Office 365 Hosted Exchange发生奇怪的未送达行
- windows-server-2008-r2 – 使用Windows更新更新Win2008R2
- windows-server-2003 – PsExec替代品
- 适用于Windows的最佳ssh程序?
- .net – 如何在Windows窗体中禁用更新表单?