工作需要,今天上午花时间看了一下INI 配置文件的相关文章,并添加到项目中。

后来想想,干脆封装成DLL 动态库,并提供给大家使用,顺便更新一下博客。^_^

INI 配置文件的格式

在早期的Windows 桌面系统中,主要是用INI 文件作为系统的配置文件,从Win95 以后开始转向使用注册表,但是还有很多系统配置是使用INI 文件的。其实,INI 文件就是简单的text 文件,只不过这种txt 文件要遵循一定的INI 文件格式。

“.ini” 就是英文 “initialization” 的头三个字母的缩写;当然INI file 的后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。

经典格式:

INI文件的格式很简单,最基本的三个要素是:parameters,sections 和 comments。

什么是 parameters?

INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value是由等号“=”隔开。name在等号的左边。

如:  name = value

什么是sections ?

所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着 ([ and ])。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的 开始就是上一个section的结束,或者是end of the file。Sections一般情况下不能被nested,当然特殊情况下也可以实现sections的嵌套。

section 如:   [section]

什么是 comments ?

在INI 文件中注释语句是以分号“;”开始的。所有的注释语句不管多长都是独占一行直到结束的。在分号和行结束符之间的所有内容都是被忽略的。

注释如:   ;comments text

当然,上面讲的都是最经典的INI文件格式,随着使用的需求INI文件的格式也出现了很多变种;

变种格式:请参考:http://en.wikipedia.org/wiki/INI_file

我的 INI 配置文件读写动态库

其实就是调用了kernel32.dll 中的 WritePrivateProfileString 和 GetPrivateProfileString 函数。

kernel32.dll是Windows 9x/Me 中非常重要的32位动态链接库文件,属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理。

INI 配置文件读写动态库 INIHelper.dll 的源码很简单,代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.IO;

namespace INIHelper{public class INIFileHelper    {private string strFileName = ""; //INI文件名        private string strFilePath = "";//获取INI文件路径

public INIFileHelper()        {            strFileName = "Config.ini"; //INI文件名//方法1:获取INI文件路径            strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;//方法2:获取INI文件路径//strFilePath = Path.GetFullPath(".\\") + strFileName;        }

public INIFileHelper(string FileName)        {            strFileName = FileName; //INI文件名            //获取INI文件路径            strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;        }

public INIFileHelper(string FullPath, string FileName)        {            strFileName = FileName; //INI文件名            strFilePath = FullPath + "\\" + strFileName;//获取INI文件路径        }

/// <summary>/// 写入INI文件/// </summary>/// <param name="section">节点名称[如[TypeName]]</param>/// <param name="key">键</param>/// <param name="val">值</param>/// <param name="filepath">文件路径</param>/// <returns></returns>        [DllImport("kernel32")]public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

/// <summary>/// 写入/// </summary>/// <param name="sectionName">section 节点名称</param>/// <param name="key">key 值</param>/// <param name="value">value 值</param>        public void Write(string sectionName, string key, string value)        {try            {//根据INI文件名设置要写入INI文件的节点名称//此处的节点名称完全可以根据实际需要进行配置                strFileName = Path.GetFileNameWithoutExtension(strFilePath);                INIFileHelper.WritePrivateProfileString(sectionName, key, value, strFilePath);            }catch            {throw new Exception("配置文件不存在或权限不足导致无法写入");            }        }

/// <summary>/// 写入默认节点"FileConfig"下的相关数据/// </summary>/// <param name="key">key 值</param>/// <param name="value">value 值</param>        public void Write(string key, string value)        {// section 节点名称使用默认值:"FileConfig"            Write("FileConfig", key, value);        }

/// <summary>/// 读取INI文件/// </summary>/// <param name="section">节点名称</param>/// <param name="key">键</param>/// <param name="def">值</param>/// <param name="retval">stringbulider对象</param>/// <param name="size">字节大小</param>/// <param name="filePath">文件路径</param>/// <returns></returns>        [DllImport("kernel32")]public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

/// <summary>/// 读取/// </summary>/// <param name="sectionName">section 节点名称</param>/// <param name="key">key 值</param>/// <returns>value 值</returns>        public string Read(string sectionName, string key)        {if (File.Exists(strFilePath)) //读取时先要判读INI文件是否存在            {                strFileName = Path.GetFileNameWithoutExtension(strFilePath);//return ContentValue(strFileName, key);                StringBuilder outValue = new StringBuilder(1024);                INIFileHelper.GetPrivateProfileString(sectionName, key, "", outValue, 1024, strFilePath);return outValue.ToString();            }else            {throw new Exception("配置文件不存在");            }        }

/// <summary>/// 读取默认节点"FileConfig"下的相关数据/// </summary>/// <param name="key">key 值</param>/// <returns>value 值</returns>        public string Read(string key)        {// section 节点名称使用默认值:"FileConfig"            return Read("FileConfig", key);        }

    }}

对 INIHelper.dll 动态库的使用和测试,代码如下:

// 获取INI文件路径
// private string strFilePath = Application.StartupPath + "\\FileConfig.ini";

//写入
private void button1_Click(object sender, EventArgs e)
{
    //test1(WinForm 测试)
    string strFilePath = "Config.ini";//获取INI文件路径
    INIFileHelper file1 = new INIFileHelper(strFilePath);
    file1.Write(label1.Text, textBox1.Text);
    file1.Write(label2.Text, textBox2.Text);
    MessageBox.Show("test1 写入完毕");

//test2
    INIFileHelper file2 = new INIFileHelper();
    file2.Write("xugang", "http://xugang.cnblogs.com");
    file2.Write("hobby", "@#$%^&*()");
    MessageBox.Show("test2 写入完毕");

//test3
    INIFileHelper file3 = new INIFileHelper("newConfig.ini");
    file3.Write("NewSection", "xugang", "http://xugang.cnblogs.com");
    file3.Write("NewSection", "hobby", "@#$%^&*()");
    MessageBox.Show("test3 写入完毕");

//test4
    string strPath = Application.StartupPath; //文件路径
    string strName = "xxx.ini";//INI文件名称

INIFileHelper file4 = new INIFileHelper(strPath, strName);
    file4.Write("NewSection", "xugang", "http://xugang.cnblogs.com");
    file4.Write("NewSection", "hobby", "@#$%^&*()");
    MessageBox.Show("test4 写入完毕");
}

//读取
private void button2_Click(object sender, EventArgs e)
{
    //test1(WinForm 测试)
    string strFilePath = "Config.ini";//获取INI文件路径
    INIFileHelper file1 = new INIFileHelper(strFilePath);
    StringBuilder str = new StringBuilder();
    str.AppendLine(file1.Read(label1.Text));
    str.AppendLine(file1.Read(label2.Text));
    MessageBox.Show(str.ToString());

//test2
    INIFileHelper file2 = new INIFileHelper();
    MessageBox.Show(file2.Read("xugang") +"   "+file2.Read("hobby"));

//test3
    INIFileHelper file3 = new INIFileHelper("newConfig.ini");
    MessageBox.Show( file3.Read("NewSection", "xugang")
           + "   " + file3.Read("NewSection", "hobby"));

//test4
    string strPath = Application.StartupPath; //文件路径
    string strName = "xxx.ini";//INI文件名称

INIFileHelper file4 = new INIFileHelper(strPath, strName);
    MessageBox.Show(file4.Read("NewSection", "xugang")
          + "   " + file4.Read("NewSection", "hobby"));
}

参考来源:

INI 配置文件的格式

INI 格式文件操作

源码下载

我的INI 配置文件读写动态库相关推荐

  1. 动态库编译通过,调用动态库函数运行出现undefined symbol

    编了一个动态库,写测试程序去调用动态库,程序编译通过,调用动态库里函数出错,通过加上动态库相关依赖库以及 extern"C"声明解决该错误以下,详细说明解决经过: 首先,刚编译好的 ...

  2. unity引用动态库的错误解决办法

    unity引用动态库的错误解决办法 引用动态库的错误 引用其他类库程序集 .NET Standard 2.0 配置文件 .NET 4.x 配置文件 引用动态库的错误 旧版unity编写的程序使用新版u ...

  3. (Murphy) Linux 动态库机制概要小结(持续更新ing)

    前言 主要介绍一些基础知识和自己学习过程中的几个误区,如果有错漏的地方,烦请指正,一起学习! 所有知识点均基于红帽系统验证过 目录 动态库编译 动态库加载 动态库配置和预加载 动态库查询 动态库统一部 ...

  4. c# 利用动态库DllImport(kernel32)读写ini文件(提供Dmo下载)

    c# 利用动态库DllImport("kernel32")读写ini文件 自从读了设计模式,真的会改变一个程序员的习惯.我觉得嘛,经验也可以从一个人的习惯看得出来,看他的代码编写习 ...

  5. python读取配置文件 分段_Python3读写ini配置文件的示例

    ini文件即Initialization File初始化文件,在应用程序及框架中常作为配置文件使用,是一种静态纯文本文件,使用记事本即可编辑. 配置文件的主要功能就是存储一批变量和变量值,在ini文件 ...

  6. 【Python教程】读写ini配置文件的详细操作

    ini文件即Initialization File初始化文件,在应用程序及框架中常作为配置文件使用,是一种静态纯文本文件,使用记事本即可编辑. 配置文件的主要功能就是存储一批变量和变量值,在ini文件 ...

  7. 易语言写c盘配置文件,易语言 读写配置项(ini配置文件)源码

    简介 易语言 读写配置项(ini配置文件)源码 源码 .版本 2 .支持库 iext .程序集 窗口程序集_启动窗口 .子程序 _按钮1_被单击 .局部变量 账号, 文本型 .局部变量 密码, 文本型 ...

  8. c读取ini配置文件_Go-INI - 超赞的Go语言INI文件操作库

    INI 文件(Initialization File)是十分常用的配置文件格式,其由节(section).键(key)和值(value)组成,编写方便,表达性强,并能实现基本的配置分组功能,被各类软件 ...

  9. C# 读写ini配置文件demo

    INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...

最新文章

  1. 关于 Caused by: java.lang.NoClassDefFoundError: com/alipay/api/AlipayApiException 解决办法
  2. filazilla搭建ftp_Windows7下利用FileZilla Server搭建ftp
  3. you should specify the `steps` argument
  4. Mellanox 8亿美元收购EZchip
  5. redis面试问题(一)
  6. 关闭文件夹或打印机共享服务器,局域网共享打印机好用,但文件夹不能访问
  7. 在oracle中处理日期大全
  8. java3d翻转纪念相册_HTML5 3D旋转相册的实现示例
  9. 【写作技巧】毕业论文写作思路
  10. 在Sql Server 2008上安装SDE 9.3
  11. Atitit 各种设计图纸分类 目录 1. Atitit 常见软件设计图纸总结 2 1.1. Uml系列图纸 2 1.2. Er图 req需求图 2 1.3. Parametric diagr
  12. LPWAN——Sigfox实战经验介绍
  13. js封装倒计时函数实现倒计时效果
  14. VBox 虚拟机完美迁移/复制(带快照)
  15. 计算机操作系统32位,电脑操作系统中32位和64位的区别
  16. Twitter首席科学家离职 高层动荡仍持续
  17. 实际开发中常用的SQL
  18. office2018自动图文集_操作快狠准!让你相见恨晚的Office快捷键
  19. iPhone 12 pro max卡槽怎么插双卡
  20. 移动硬盘提示需要格式化怎么办?数据可以恢复吗

热门文章

  1. 修改USB固件库的Customer_HID例程
  2. hdu 2363(最短路+枚举)
  3. 测地膨胀和膨胀重建—lhMorpRDilate
  4. 诗与远方:无题(七十六)
  5. Struts2的标签概述
  6. docker k8s helm常用命令梳理
  7. 从无到有整合SpringMVC-MyBatis项目(1):搭建JavaWeb项目
  8. mysql 存储微信昵称乱码_MYSQL 保存微信昵称特殊字符报错解决方法-设置编码集为utf8mb4的方法...
  9. 【POJ 1151】Atlantis
  10. Docker:unauthorized: incorrect username or password.