zoukankan      html  css  js  c++  java
  • [原创]INI文件的读写操作

    INI文件的读写操作

     

    在程序开发中,很多人会把参数保存为ini文件格式,ini文件的一个好处是可以保存为一个结构体主结构,如

    [User]

    Name=test

    UserId=test

    [Server]

    ServerIp=127.0.0.1

    ServerPort=80

    ……

     

    很方便也很容易区分,而且不同节点下的名称可以重复。读取Name时只需要读取User节点下的Name

    ini文件的读写是需要调用windowsAPI来操作的。该API方法是在kernel32.dll中。

     

    ini文件调用

    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder refVal,int size, string filePath);

    参数说明:

    sectionINI文件中的段落名称;

    keyINI文件中的关键字;

    def:无法读取时候时候的缺省数值;

    retVal:读取数值;

    size:数值的大小;

    filePathINI文件的完整路径和名称

     

    ini文件调用

     [DllImport("kernel32")]

            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    参数说明:

    sectionINI文件中的段落;

    keyINI文件中的关键字;

    valINI文件中关键字的数值;

    filePathINI文件的完整的路径和名称

     

           

    具体代码如下:

     

    public class IniFile
    {

        
    private static string sIniFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config.ini");

      //参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称
      [DllImport("kernel32")]
      
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);      

       
    //参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;
       //size:数值的大小;filePath:INI文件的完整路径和名称
       [DllImport("kernel32")]
        
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder refVal, int size, string filePath);      
    public static string sUserId =””;
    public static string sName =””;
    public static string sServerIp =””;
    public static string sServerPort =””; 

    #region 写入配置文件节
    /// <summary>
    /// 写入配置文件节
    /// </summary>
    public static long WriteProfileString(string sSection, string sKey, string sVal, string sFileName)
    {
         
    return WritePrivateProfileString(sSection, sKey, sVal, sFileName);
    }

    #endregion 

    #region 读取配置文件节
    /// <summary>
    /// 读取配置文件节
    /// </summary>
    public static long GetProfileString(string sSection, string sKey, ref string refVal, string sFileName)
    {
        StringBuilder sVal 
    = new StringBuilder();
        
    long lResult = GetPrivateProfileString(sSection, sKey, refVal, sVal, 255, sFileName);
        refVal 
    = sVal.ToString();
        
    return lResult;
    }

    #endregion 

    #region 写入INI文件节
    /// <summary>
    /// 写入INI文件节
    /// </summary>
    public static long WriteIniFileString(string sSection, string sKey, string sVal)
    {
        
    if (string.IsNullOrEmpty(sVal))
        {
             
    return -1;
        }

        
    return WritePrivateProfileString(sSection, sKey, sVal, sIniFile);
    }

    #endregion 

    #region 读取INI文件节
    /// <summary>
    /// 读取INI文件节
    /// </summary>
    public static long GetIniFileString(string sSection, string sKey, ref string refVal)
    {
        StringBuilder sVal 
    = new StringBuilder(20482048);
        
    long lResult = GetPrivateProfileString(sSection, sKey, refVal, sVal, 1024, sIniFile);

        refVal 
    = sVal.ToString();

        
    return lResult;

    }

    #endregion


    #region 读取配置文件
    /// <summary>
    /// 读取配置文件
    /// </summary>
    public static bool ReadIniFile()
    {
        
    try
        {
           
    // User
           GetIniFileString(
    "User""UserId"ref sUserId);
           GetIniFileString(
    "User""Name"ref sName); 

           
    // Server
           GetIniFileString(
    "Server""ServerIp"ref sServerIp);
           GetIniFileString(
    "Server""ServerPort"ref sPort);
           
    return true;
        }
        
    catch (Exception ex)
        {
            
    return false;
        }
    }

    #endregion 

    #region 写入配置文件
    /// <summary>
    /// 写入配置文件
    /// </summary>
    public static bool WriteIniFile()
    {
       
    try
       {
            
    // User
            WriteIniFileString(
    "User""UserId", sUserId);
            WriteIniFileString(
    "User""Name", sName); 

            
    // Server
            WriteIniFileString(
    "Server""ServerIp", sServerIp);
            WriteIniFileString(
    "Server""ServerPort", sPort);
            
    return true;
        }
        
    catch (Exception ex)
        {
            
    return false;
        }
    }

    #endregion

    }

    作者:chhuic

    出处:http://chhuic.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    HDU 4778 内存搜索&amp;如压力
    cocos2dx-2.x CCFileUtils文件管理分析(2)
    开源 自由 java CMS
    1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup
    什么是PV,UV。
    Python爬虫框架Scrapy获得定向打击批量招聘信息
    采用ToolRunner执行Hadoop基本面分析程序
    编辑时snapping的添加
    利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
    由图层判断数据源类型
  • 原文地址:https://www.cnblogs.com/chhuic/p/1810358.html
Copyright © 2011-2022 走看看