• 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:	est	est.exe -> "C:	est", "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:	est	est.exe 得到 "C:	est", "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("    调整进程当前目录为程序可执行文件所在目录 
    ");        
        printf(" - http://blog.csdn.net/morewindows/article/details/8683519 -
    ");
        printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --
    
    ");  
    
        char szCurrentDirectory[MAX_PATH];
        GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
        printf("进程当前目录为: 
    %s
    ", szCurrentDirectory);
    
        AdjustProcessCurrentDirectory();
    
        GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
        printf("
    调整后,进程当前目录为: 
    %s
    ", szCurrentDirectory);
        getch();
        return 0;
    }

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

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

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

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

    欢迎关注微博:http://weibo.com/MoreWindows

  • 相关阅读:
    node.js爬虫杭州房产销售及数据可视化
    webpack1.x 升级到 webpack2.x 英文文档翻译
    一机双屏和双屏通信方案总结
    Linux 利用hosts.deny 防止暴力破解ssh
    Linux 之rsyslog+LogAnalyzer 日志收集系统
    count和distinct
    排序算法 — 冒泡排序
    排序算法 — 插入排序
    排序算法 — 选择排序
    CRLF will be replaced by LF in XXX when git commit
  • 原文地址:https://www.cnblogs.com/happykoukou/p/8979983.html
Copyright © 2020-2023  润新知