zoukankan      html  css  js  c++  java
  • 配置文件操作(获取路径、及取得相应数据)

    1.获取当前dll或者exe的路径:
    
    方法一、
    
    void  GetNowDllPath(CString & strPath)
    {
    	Char szPath[100]={0};
    	CWinApp *pApp = AfxGetApp();
    	
    	//szPath结果看下面示例截图,一目了然;pApp->m_hInstance为当前模块的句柄
    
    	GetModuleFileName(pApp? pApp->m_hInstance: NULL, szPath, sizeof(szPath));
    
    	Char *pDest = strrchr(szPath, ‘\’);//找到字符串szPath中最后一个单斜线的位置
    
    	If(pDest != NULL)
    	{
    		*pDest = ‘’;//最后一个单斜线的位置,置为结束符
    	}
    
    	strPath = szPath;
    }
    
     
    
    方法二、
    
    void  GetNowDllPath(CString & strPath)
    {
      char szPath[260]={0};
    
      GetCurrentDirectory(511,szPath);
    
      CString  dllHome;
    
      dllHome = szPath;
    
      int p1 = dllHome.ReverseFind('/');
    
      int p2 = dllHome.ReverseFind('\');
    
      int pos = max(p1,p2);
    
      if(pos > 0)
    
        dllHome = dllHome.Left(pos+1);
    
      strPath = dllHome;
    }
    
     
    
    GetModuleFileName 与 GetCurrentDirectory 区别:
    
      GetModuleFileName 函数指定当前进程模块的路径.它仅仅操作当前进程下的模块.如果想获取其他进程下的模块信息, 则需使用 GetModuleFileNameEx 函数。
    
      GetCurrentDirectory 返回当前进程的当前目录,并不一定返回你的应用程序的目录。如果你在应用程序中调用了打开文件对话框,你选择了一个文件,那么,这个文件所在的目录就成了当前进程的当前目录了。
    
     
    
     
    
    2.获取当前DLL下配置文件的信息:
    
    	CString HomePath;
    
    	GetNowDllPath(HomePath);
    
    	CString InitPath = HomePath + “\scistock.ini”;
    
    	Char g_szMongoDBHost[128]={0};
    
    	Int g_nMongoDBPort = 0;
    
    	GetPrivateProfileString(“MongoDB”, ”HOST”, “127.0.0.1”,  g_szMongoDBHost , 128, IniPath);
    
    	g_nMongoDBPort = GetPrivateProfileInt(“MongoDB”, “PORT”, 27017, IniPath);
    	
    

      

    示例图片:

     

    GetPrivateProfileString、GetPrivateProfileInt的原型:
    
    DWORD GetPrivateProfileString(
    
    LPCTSTR lpAppName, //配置文件的section名
    
    LPCTSTR lpKeyName, //配置文件的key名
    
    LPCTSTR lpDefault, //默认返回值
    
    LPTSTR lpReturnedString, //返回值
    
    DWORD nSize, //返回值最大长度
    
    LPCTSTR lpFileName //配置文件路径
    
    );
    
     
    
    UINT WINAPI GetPrivateProfileInt
    
    (
    
    _In_LPCTSTR lpAppName, //配置文件的section名
    
    _In_LPCTSTR lpKeyName, //配置文件的key名
    
    _In_INT nDefault, //默认返回值
    
    _In_LPCTSTR lpFileName //配置文件路径
    
    );
    

      

    配置文件截图:

     

  • 相关阅读:
    部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    [.Net MVC] Win7下IIS部署
    CSS的4种引入方式及优先级
    阿里巴巴图标库全部下载
    div的默认position值是静态的static
    阿里巴巴图标库iconfont上传svg后,显示不了图片
    ext.net单元格内容换行显示
    WEB内容换行
    SQL修改日期类型字段为字符串类型
  • 原文地址:https://www.cnblogs.com/SZxiaochun/p/6376988.html
Copyright © 2011-2022 走看看