zoukankan      html  css  js  c++  java
  • 获得当前程序路径

    1.返回当前程序文件全文件名

    char pBuf[MAX_PATH];//存放路径的变量
    GetCurrentDirectory(MAX_PATH,pBuf);//获取程序的当前目录
    strcat(pBuf,"\\");
    strcat(pBuf,AfxGetApp()->m_pszExeName);

    strcat(pBuf,".exe");//获取程序全文件名
    CString filename;
    filename = pBuf;//char*转换为CString如此简单
    AfxMessageBox(filename);

    2.方法2:

    char path[MAX_PATH];   //MAX_PATH在API中定义为260

    GetModuleFileName(NULL,path,MAX_PATH);

    CString str=path;

    AfxMessageBox(str);

    3.方法3

    CString str = _pgmptr;

    AfxMessageBox(str);

    //_pgmptr为char*类型,系统定义好的。

    扩展:

    char drive[_MAX_DRIVE];

    char dir[_MAX_DIR];

    char fname[_MAX_FNAME];

    char ext[_MAX_EXT];

    _splitpath(_pgmptr, drive, dir, fname, ext ); // _splitpath对_pgmptr分割

    CString str;

    str += drive;

    str += dir;

    str += fname;

    str += ext;

    AfxMessageBox(str);

    4.方法4

    CString str = GetCommandLine();

    AfxMessageBox(str);

    5.方法5

    CString   path=__argv[0];//取得路径  

    AfxMessageBox(path);

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    如果要获得当前程序目录

    char f_path[100];

    GetCurrentDirectory(100,f_path);

    AfxMessageBox(f_path);

    方法2:

    char drive[_MAX_DRIVE];

    char dir[_MAX_DIR];

    char fname[_MAX_FNAME];

    char ext[_MAX_EXT];

    // _makepath( _pgmptr, "c", "\\sample\\crt\\", "makepath", "c" );  //可以自己创建一个路径

    _splitpath( _pgmptr, drive, dir, fname, ext );

    _makepath( _pgmptr, drive, dir, NULL, NULL );

    MessageBox(_pgmptr);

    或者将最后两句改为:

    CString str = “”;

    str = str + drive + dir;

    MessageBox(str);

  • 相关阅读:
    228. Summary Ranges
    227. Basic Calculator II
    224. Basic Calculator
    222. Count Complete Tree Nodes
    223. Rectangle Area
    221. Maximal Square
    220. Contains Duplicate III
    219. Contains Duplicate II
    217. Contains Duplicate
    Java编程思想 4th 第4章 控制执行流程
  • 原文地址:https://www.cnblogs.com/lxshanye/p/3088607.html
Copyright © 2011-2022 走看看