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

 

    调整进程当前目录为程序可执行文件所在目录是个非常实用的方法。为了更加的让代码复用,本文将调整进程当前目录为程序可执行文件所在目录这一功能封装为三个实用函数——

1SplitPathFileName

这个函数将文件全名(带路径)分解成路径名,文件名,后缀名。

2GetProcessPathNameAndFileName

得到当前进程可执行文件的路径名,文件名,后缀名。

3AdjustProcessCurrentDirectory

调整进程当前目录为程序可执行文件所在目录

 

各函数使用示范可以参见如下程序:

[cpp] view plaincopy
  1. //调整进程当前目录为程序可执行文件所在目录  
  2. //http://blog.csdn.net/morewindows/article/details/8683519  
  3. //By MoreWindows( http://blog.csdn.net/MoreWindows )  
  4. #include <windows.h>  
  5. #include <stdio.h>  
  6. #include <conio.h>  
  7.   
  8. //将文件全名(带路径)分解成路径名,文件名,后缀名  
  9. //C:\test\test.exe -> "C:\test\", "test", ".exe"  
  10. //By MoreWindows( http://blog.csdn.net/MoreWindows )  
  11. void SplitPathFileName(const char *pstrPathFileName, char *pstrPath, char *pstrFileName, char *pstrExtName)  
  12. {  
  13.     if (pstrPath != NULL)  
  14.     {  
  15.         char szTemp[MAX_PATH];  
  16.         _splitpath(pstrPathFileName, pstrPath, szTemp, pstrFileName, pstrExtName);  
  17.         strcat(pstrPath, szTemp);  
  18.     }  
  19.     else  
  20.     {  
  21.         _splitpath(pstrPathFileName, NULL, NULL, pstrFileName, pstrExtName);  
  22.     }  
  23. }  
  24.   
  25. //得到当前进程可执行文件的路径名,文件名,后缀名  
  26. //如运行C:\test\test.exe 得到 "C:\test\", "test", ".exe"  
  27. //By MoreWindows( http://blog.csdn.net/MoreWindows )  
  28. BOOL GetProcessPathNameAndFileName(char *pstrPath, char *pstrFileName, char *pstrExtName)  
  29. {  
  30.     char szExeFilePathFileName[MAX_PATH];  
  31.     if (GetModuleFileName(NULL, szExeFilePathFileName, MAX_PATH) == 0)  
  32.         return FALSE;  
  33.       
  34.     SplitPathFileName(szExeFilePathFileName, pstrPath, pstrFileName,pstrExtName);  
  35.     return TRUE;  
  36. }  
  37.   
  38. //调整进程当前目录为程序可执行文件所在目录  
  39. //By MoreWindows( http://blog.csdn.net/MoreWindows )  
  40. BOOL AdjustProcessCurrentDirectory()  
  41. {  
  42.     char szPathName[MAX_PATH];  
  43.     GetProcessPathNameAndFileName(szPathName, NULL, NULL);  
  44.     return SetCurrentDirectory(szPathName);  
  45. }  
  46.   
  47.   
  48.   
  49. int main()  
  50. {  
  51.     printf("    调整进程当前目录为程序可执行文件所在目录 \n");          
  52.     printf(" - http://blog.csdn.net/morewindows/article/details/8683519 -\n");  
  53.     printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");    
  54.   
  55.     char szCurrentDirectory[MAX_PATH];  
  56.     GetCurrentDirectory(MAX_PATH, szCurrentDirectory);  
  57.     printf("进程当前目录为: \n%s\n", szCurrentDirectory);  
  58.   
  59.     AdjustProcessCurrentDirectory();  
  60.   
  61.     GetCurrentDirectory(MAX_PATH, szCurrentDirectory);  
  62.     printf("\n调整后,进程当前目录为: \n%s\n", szCurrentDirectory);  
  63.     getch();  
  64.     return 0;  
  65. }  

通过CMD来调用这个程序看看。

 

由图可以看出,程序的当前目录已经被调整到程序可执行文件所在目录了。

 

附1:得到程序所在目录还可以使用PathRemoveFileSpec函数。20130507

 

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165721

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8683519

0 0
原创粉丝点击