zoukankan      html  css  js  c++  java
  • VC 获 取 当前程序运行路径的几种方法

    1.使用APi函数GetModuleFileName

    char path[MAX_PATH];
    GetModuleFileName(NULL, path, MAX_PATH);        //获取到完整路径如:E:Toolsqq.exe
    *strrchr(path,'\') = '';                                                      //截取路径E:Tools

    2.MFC

    char path[MAX_PATH];
    memcpy(path, AfxGetApp()->m_pszHelpFilePath, MAX_PATH)       //获取到完整路径如:E:Toolsqq.hlp
    *strrchr(path,'\') = '';                                                                              //截取路径E:Tools

    3 用这个函数也可以做到截取路径

    PathRemoveFileSpec(LPTSTR pszPath)

    例:

    LPTSTR GetProgramDir(int nBufferLength, LPTSTR lpBuffer)
    {
        DWORD dwReturn = 0;
    LPTSTR tszSlash;

        if (nBufferLength <= 0 || lpBuffer == NULL)
            return NULL;

        dwReturn = ::GetModuleFileName(NULL, lpBuffer, nBufferLength);
    if (dwReturn <= nBufferLength)
        {
        PathRemoveFileSpec(lpBuffer); 
        tszSlash = lpBuffer;
        }

        return tszSlash;
    }

    2..

    string GetPPath()                 //取程序运行的当前路径
    {
    TCHAR exeFullPath[MAX_PATH]; // MAX_PATH
    GetModuleFileName(NULL,exeFullPath,MAX_PATH);//得到程序模块名称,全路径

    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];

    _splitpath(exeFullPath, drive, dir, NULL,NULL);

    string PragramPath(drive);
    string TempPath(dir);
    PragramPath += TempPath ;
    cout<< PragramPath<<endl;
    return PragramPath;
    }

     

    3.unicode 工程

     

     TCHAR AppPath[256] = {0};

    char g_strAppPath[256] = {0};

     ::GetModuleFileName(NULL,AppPath, MAX_PATH);
     

     WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)AppPath, -1, g_strAppPath, 256, 0, 0);

     

    *strrchr(g_strAppPath,'\') = ''; 
     

  • 相关阅读:
    new JSONObject()报空指针异常
    转:修改Fckeditor 2.6 增加插入Mp3等多媒体文件功能
    执行更新操作时,timestamp类型的字段自动被更新为了系统当前日期
    读取某个文件的位置
    myeclipse配置注释
    为eclipse 的 hibernate 配置文件加提示
    hibernate3与hibernate4
    查看eclipse版本
    Eclipse编译没有class文件生成
    严重: Error loading WebappClassLoader context:
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621006.html
Copyright © 2011-2022 走看看