zoukankan      html  css  js  c++  java
  • VC得到当前目录与得到应用程序目录的一个应用

      得到当前目录的函数:    GetCurrentDirectory
      得到应用程序目录的函数是: GetModuleFileName
      用法都很简单,不知道就看MSDN吧。
            
      我先用这2个函数分别写一个实现同一个功能的函数。

    代码
    以下是代码片段:
     
    // 函 数 名:ReadIni
    // 功能描述:读配置文件database.ini(用来配置oracle数据库的用户名、密码和服务名的),成功返回true,否则返回false
    // 输入参数:void
    // 输出参数:bool
    // 创建日期:2006-5-25
    // 修改日期:2006-5-25
    // 作 者:joinclear
    // 附加说明:无
    bool ReadIni()
    {
    char databuf[1024];
    char filepath[1024];
    memset(databuf,
    0,1024);
    memset(filepath,
    0,1024);
    GetCurrentDirectory(
    1024,filepath);
    strcat(filepath ,
    "\\" );
    strcat(filepath ,
    "database.ini" );

    CFile file;
    if ( !file.Open(filepath,CFile::modeRead))
    {
    return false;
    }
    file.Close();

    memset(databuf,
    0,1024);
    GetPrivateProfileString(
    "oracle","user id","",databuf,1024,filepath);
    m_sUserId
    = databuf;

    memset(databuf,
    0,1024);
    GetPrivateProfileString(
    "oracle","password","",databuf,1024,filepath);
    m_sPassword
    = databuf;

    memset(databuf,
    0,1024);
    GetPrivateProfileString(
    "oracle","data source","",databuf,1024,filepath);
    m_sDataSource
    = databuf;

    memset(databuf,
    0,1024);
    memset(filepath,
    0,1024);

    return true;
    }

    bool ReadIni()
    {

    char databuf[1024];

    CString sPath;
    GetModuleFileName(NULL,sPath.GetBufferSetLength(
    1023),1024);
    sPath.ReleaseBuffer();
    int nPos;
    nPos
    = sPath.ReverseFind(’\\’);
    sPath
    = sPath.Left(nPos);

       CString str
    = sPath + "\\database.ini";

    char filepath[1024];
    strcpy(filepath,(LPSTR)(LPCTSTR)str);

    CFile file;
    if ( !file.Open(filepath,CFile::modeRead))
    {
    return false;
    }
    file.Close();

    memset(databuf,
    0,1024);
    GetPrivateProfileString(
    "oracle","user id","",databuf,1024,filepath);
    m_sUserId
    = databuf;

    memset(databuf,
    0,1024);
    GetPrivateProfileString(
    "oracle","password","",databuf,1024,filepath);
    m_sPassword
    = databuf;

    memset(databuf,
    0,1024);
    GetPrivateProfileString(
    "oracle","data source","",databuf,1024,filepath);
    m_sDataSource
    = databuf;

    memset(databuf,
    0,1024);
    memset(filepath,
    0,1024);

    return true;
    }

      你用这2个函数分别执行一下,发现并没有任何的不一样。
        
        但是当你在同一个程序里面,哪怕是不同的界面,如果你打开一个文件选择或保存对话框的时候,用GetCurrentDirectory函数的这种方法就不行了。因为当前目录被你的文件选择或保存对话框修改了。
        
        这时用GetModuleFileName函数的方法就很有必要了,但要注意的是GetModuleFileName得到的应用路径是包括EXE名的,所以EXE名还是要去掉的。(前提是database.ini和EXE文件是在同个目录下)

  • 相关阅读:
    上传图片,将图片保存在腾讯云(2种方式)
    由ping所引发的思考~
    php面试上机题(2018-3-3)
    【八】jqeury之click事件[添加及删除数据]
    【七】jquery之属性attr、 removeAttr、prop[全选全不选及反选]
    【六】jquery之HTML代码/文本/值[下拉列表框、多选框、单选框的选中]
    【五】jquery之事件(focus事件与blur事件)[提示语的出现及消失时机]
    小白懂算法之基数排序
    mysql_sql199语法介绍
    Python基本编程快速入门
  • 原文地址:https://www.cnblogs.com/joinclear/p/1882218.html
Copyright © 2011-2022 走看看