关于C#操作INI文件的总结

INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下:
[Section1]
key = value2
key = value2
……
[Section2]
key = value1
key = value2
……
文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值(value)。Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePrivateProfileString()分别实现了对INI文件的读写操作,他们位于kernel32.dll下。
但是令人遗憾的是C#所使用的.NET框架下的公共类库并没有提供直接操作INI文件的类,所以唯一比较理想的方法就是调用API函数。
然后,.Net框架下的类库是基于托管代码的,而API函数是基于非托管代码的,(在运行库的控制下执行的代码称作托管代码。相反,在运行库之外运行的代码称作非托管代码。)如何实现托管代码与非托管代码之间的操作呢?.Net框架的System.Runtime.InteropServices命名空间下提供各种各样支持COM interop及平台调用服务的成员,其中最重要的属性之一DllImportAttribute可以用来定义用于访问非托管API的平台调用方法,它提供了对从非托管DLL导出的函数进行调用所必需的信息。下面就来看一下如何实现C#与API函数的互操作。
读操作: [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath); section:要读取的段落名 key: 要读取的键 defVal: 读取异常的情况下的缺省值 retVal: key所对应的值,如果该key不存在则返回空值 size: 值允许的大小 filePath: INI文件的完整路径和文件名
写操作: [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); section: 要写入的段落名 key: 要写入的键,如果该key存在则覆盖写入 val: key所对应的值 filePath: INI文件的完整路径和文件名
这样,在就可以使用对他们的调用,用常规的方式定义一个名为IniFile类: using System; using System.Runtime.InteropServices; using System.Text; namespace IPVOD.Hotel.Remoting { /// <summary> /// INI文件的操作类 /// </summary> public class IniFile { public string Path; public IniFile(string path) { this.Path = path; } 声明读写INI文件的API函数 /// <summary> /// 写INI文件 /// </summary> /// <param name="section">段落</param> /// <param name="key">键</param> /// <param name="iValue">值</param> public void IniWriteValue(string section, string key, string iValue) { WritePrivateProfileString(section, key, iValue, this.Path); } /// <summary> /// 读取INI文件 /// </summary> /// <param name="section">段落</param> /// <param name="key">键</param> /// <returns>返回的键值</returns> public string IniReadValue(string section, string key) { StringBuilder temp = new StringBuilder(); int i = GetPrivateProfileString(section, key, "", temp, , this.Path); return temp.ToString(); } /// <summary> /// 读取INI文件 /// </summary> /// <param name="Section">段,格式[]</param> /// <param name="Key">键</param> /// <returns>返回byte类型的section组或键值组</returns> public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[]; int i = GetPrivateProfileString(section, key, "", temp, , this.Path); return temp; } } } 注意:我增加了DLL导出的函数GetPrivateProfileString的重载,说明如下: [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); section:要读取的段落名 key: 要读取的键 defVal: 读取异常的情况下的缺省值 retVal: 此参数类型不是string,而是Byte[]用于返回byte类型的section组或键值组。 size: 值允许的大小 filePath: INI文件的完整路径和文件名 下面看一下具体实例化IniFile类的操作:
//path为ini文件的物理路径
IniFile ini = new IniFile(path);
//读取ini文件的所有段落名
byte[] allSection = ini.IniReadValues(null, null); 通过如下方式转换byte[]类型为string[]数组类型
string[] sectionList;
ASCIIEncoding ascii = new ASCIIEncoding();
//获取自定义设置section中的所有key,byte[]类型
sectionByte = ini.IniReadValues("personal", null);
//编码所有key的string类型
sections = ascii.GetString(sectionByte);
//获取key的数组
sectionList = sections.Split(new char[]{'\0'}); //读取ini文件personal段落的所有键名,返回byte[]类型
byte[] sectionByte = ini.IniReadValues("personal", null); //读取ini文件evideo段落的MODEL键值
model = ini.IniReadValue("evideo", "MODEL"); //将值eth0写入ini文件evideo段落的DEVICE键
ini.IniWriteValue("evideo", "DEVICE", "eth0");
即:
[evideo]
DEVICE = eth0 //删除ini文件下personal段落下的所有键
ini.IniWriteValue("personal", null, null); //删除ini文件下所有段落
ini.IniWriteValue(null, null, null); 这样就实现了C#对ini文件包括段落section,键key,键值value的基本上所有操作,当然这只是简单的举例,不是详细的实现,欢迎随时提出任何疑问和建议。

如何在C#中读写INI文件

INI文件就是扩展名为“ini”的文件。在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。但在某些场合,INI文件还拥有其不可替代的地位。本文就来探讨一下C#是如何对INI进行读写操作。

INI文件的结构
INI文件是一种按照特点方式排列的文本文件。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下: [Section1] KeyWord1 = Valuel KeyWord2 = Value2 …… [Section2] KeyWord3 = Value3 KeyWord4 = Value4 本文中介绍的程序设计及运行环境:
● 微软视窗2000 高级服务器版
● .Net Framework SDK正式版 C#和Win32 API函数
C#并不像C++,拥有属于自己的类库。C#使用的类库是.Net框架为所有.Net程序开发提供的一个共有的类库——.Net FrameWork SDK。虽然.Net FrameWork SDK内容十分庞大,功能也非常强大,但还不能面面俱到,至少它并没有提供直接操作INI文件所需要的相关的类。在本文中,C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()和GetPrivateProfileString()函数。这二个函数都位于“kernel32.dll”文件中。
我们知道在C#中使用的类库都是托管代码(Managed Code)文件,而Win32的API函数所处的文件,都是非托管代码(Unmanaged Code)文件。这就导致了在C#中不可能直接使用这些非托管代码文件中的函数。好在.Net框架为了保持对下的兼容,也为了充分利用以前的资源,提出了互操作,通过互操作可以实现对Win32的API函数的调用。互操作不仅适用于Win32的API函数,还可以用来访问托管的COM对象。C#中对Win32的API函数的互操作是通过命名空间“System.Runtime.InteropServices”中的“DllImport”特征类来实现的。它的主要作用是指示此属性化方法是作为非托管DLL的输出实现的。下面代码就是在C#利用命名空间“System.Runtime.InteropServices”中的“DllImport”特征类申明上面二个Win32的API函数:
C#申明INI文件的写操作函数WritePrivateProfileString(): [ DllImport ( "kernel32" ) ] private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ; 参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
C#申明INI文件的读操作函数GetPrivateProfileString(): [ DllImport ( "kernel32" ) ] private static extern int GetPrivateProfileString ( string section , string key , string def , StringBuilder retVal , int size , string filePath ) ; 参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。 C#中读写INI文件的关键步骤和解决方法
C#对INI文件进行写操作:
对INI文件进行写操作,是通过组件button2的"Click"事件来实现的。这里有一点应该注意,当在调用WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息。下面是button2组件的"Click"事件对应的代码清单: private void button2_Click ( object sender , System.EventArgs e ) { string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; string keyValue = textBox4.Text ; WritePrivateProfileString ( section , key , keyValue , FileName ) ; MessageBox.Show ( "成功写入INI文件!" , "信息" ) ; } C#对INI文件进行读操作:
正确读取INI的必须满足三个前提:INI文件的全路径、段落名称和关键字名称。否则就无法正确读取。你可以设定读取不成功后的缺省数值,在下面的程序中,为了直观设定的是“无法读取对应数值!”字符串,读取INI文件是通过button3组件的“Click”事件来实现的,下面是其对应的代码清单: private void button3_Click ( object sender , System.EventArgs e ) { StringBuilder temp = new StringBuilder ( ) ; string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; int i = GetPrivateProfileString ( section , key ,"无法读取对应数值!", temp , , FileName ) ; //显示读取的数值 textBox4.Text = temp.ToString ( ) ; } C#操作INI文件的完整源代码(ini.cs)和运行界面
通过上面的这些介绍,我们不难得到用C#操作INI文件的完整代码清单(ini.cs),具体如下: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Runtime.InteropServices ; using System.Text ; namespace C_操作INI文件__写操作 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1 ; private System.Windows.Forms.TextBox textBox1 ; private System.Windows.Forms.Button button2 ; private System.Windows.Forms.TextBox textBox2 ; private System.Windows.Forms.TextBox textBox3 ; private System.Windows.Forms.TextBox textBox4 ; private System.Windows.Forms.Label label1 ; private System.Windows.Forms.Label label2 ; private System.Windows.Forms.Label label3 ; private System.Windows.Forms.Button button3 ; private System.Windows.Forms.OpenFileDialog openFileDialog1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { InitializeComponent ( ) ; } protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } [ DllImport ( "kernel32" ) ] private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ; [ DllImport ( "kernel32" ) ] private static extern int GetPrivateProfileString ( string section , string key , string def , StringBuilder retVal , int size , string filePath ) ; private void InitializeComponent ( ) { this.button1 = new System.Windows.Forms.Button ( ) ; this.textBox1 = new System.Windows.Forms.TextBox ( ) ; this.button2 = new System.Windows.Forms.Button ( ) ; this.textBox2 = new System.Windows.Forms.TextBox ( ) ; this.textBox3 = new System.Windows.Forms.TextBox ( ) ; this.textBox4 = new System.Windows.Forms.TextBox ( ) ; this.label1 = new System.Windows.Forms.Label ( ) ; this.label2 = new System.Windows.Forms.Label ( ) ; this.label3 = new System.Windows.Forms.Label ( ) ; this.button3 = new System.Windows.Forms.Button ( ) ; this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog ( ) ; this.SuspendLayout ( ) ; this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; this.button1.Location = new System.Drawing.Point ( , ) ; this.button1.Name = "button1" ; this.button1.Size = new System.Drawing.Size ( , ) ; this.button1.TabIndex = ; this.button1.Text = "选择INI文件" ; this.button1.Click += new System.EventHandler ( this.button1_Click ) ; this.textBox1.Location = new System.Drawing.Point ( , ) ; this.textBox1.Name = "textBox1" ; this.textBox1.Size = new System.Drawing.Size ( , ) ; this.textBox1.TabIndex = ; this.textBox1.Text = "" ; this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; this.button2.Location = new System.Drawing.Point ( , ) ; this.button2.Name = "button2" ; this.button2.Size = new System.Drawing.Size ( , ) ; this.button2.TabIndex = ; this.button2.Text = "写入INI文件" ; this.button2.Click += new System.EventHandler ( this.button2_Click ) ; this.textBox2.Location = new System.Drawing.Point ( , ) ; this.textBox2.Name = "textBox2" ; this.textBox2.Size = new System.Drawing.Size ( , ) ; this.textBox2.TabIndex = ; this.textBox2.Text = "" ; this.textBox3.Location = new System.Drawing.Point ( , ) ; this.textBox3.Name = "textBox3" ; this.textBox3.Size = new System.Drawing.Size ( , ) ; this.textBox3.TabIndex = ; this.textBox3.Text = "" ; this.textBox4.Location = new System.Drawing.Point ( , ) ; this.textBox4.Name = "textBox4" ; this.textBox4.Size = new System.Drawing.Size ( , ) ; this.textBox4.TabIndex = ; this.textBox4.Text = "" ; this.label1.Location = new System.Drawing.Point ( , ) ; this.label1.Name = "label1" ; this.label1.TabIndex = ; this.label1.Text = "段落名称:" ; this.label2.Location = new System.Drawing.Point ( , ) ; this.label2.Name = "label2" ; this.label2.TabIndex = ; this.label2.Text = "关键字:" ; this.label3.Location = new System.Drawing.Point ( , ) ; this.label3.Name = "label3" ; this.label3.TabIndex = ; this.label3.Text = "关键字数值:" ; this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; this.button3.Location = new System.Drawing.Point ( , ) ; this.button3.Name = "button3" ; this.button3.Size = new System.Drawing.Size ( , ) ; this.button3.TabIndex = ; this.button3.Text = "读取INI数值" ; this.button3.Click += new System.EventHandler ( this.button3_Click ) ; this.openFileDialog1.Filter = "INI 文件|*.ini" ; this.AutoScaleBaseSize = new System.Drawing.Size ( , ) ; this.ClientSize = new System.Drawing.Size ( , ) ; this.Controls.AddRange ( new System.Windows.Forms.Control [ ] { this.button3 , this.textBox4 , this.textBox3 , this.textBox2 , this.button2 , this.textBox1 , this.button1 , this.label3 , this.label2 , this.label1 } ) ; this.MaximizeBox = false ; this.Name = "Form1" ; this.Text = "C#操作INI文件--写操作" ; this.ResumeLayout ( false ) ; } [STAThread] static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { openFileDialog1.ShowDialog ( ) ; textBox1.Text = openFileDialog1.FileName ; } //写入INI文件 private void button2_Click ( object sender , System.EventArgs e ) { string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; string keyValue = textBox4.Text ; WritePrivateProfileString ( section , key , keyValue , FileName ) ; MessageBox.Show ( "成功写入INI文件!" , "信息" ) ; } //读取指定INI文件的特定段落中的关键字的数值 private void button3_Click ( object sender , System.EventArgs e ) { StringBuilder temp = new StringBuilder ( 255 ) ; string FileName = textBox1.Text ; string section = textBox2.Text ; string key = textBox3.Text ; int i = GetPrivateProfileString ( section , key , "无法读取对应数值!" , emp , 255 , FileName ) ; //显示读取的数值 textBox4.Text = temp.ToString ( ) ; } } } 下图是ini.cs编译后的运行界面: 图1:C#操作INI文件的运行界面 总结
通过上面的这些介绍,可以看成C#操作INI文件的过程,其实就是C#调用Win32的API函数的过程。掌握了如何在C#申明Win32的API函数,再来操作INI就显得非常简单。

最新文章

  1. PHP build notes - WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: 3.0, min: 204, excluded: 3.0).
  2. liToSpan
  3. 转: ORACLE索引介绍和使用
  4. Qt Quick实现的疯狂算数游戏
  5. 【HDOJ】2988 Dark roads
  6. oracle 异常管理
  7. 使用Mina框架开发 QQ Android 客户端
  8. web简易MP3播放插件 Aplayer篇章一
  9. JournalNode failed to restart
  10. InnoSetup 脚本打包及管理员权限设置
  11. ubuntu 12.04启用休眠
  12. (转) Face-Resources
  13. [工具]cmd命令大全
  14. Asp.net(C#)常用正则表达式封装
  15. zip压缩工具 tar打包 打包并压缩
  16. 在ListView中使用GridView, Style这样写:
  17. jenkins部署时遇到“似乎无法联网”,导致无法安装默认插件的解决方案
  18. windows下查看端口是否被占,以及端口被哪个程序占用
  19. DA14580_583_DK_II开发板入门笔记
  20. Cocos2d-x-Lua (2.x)脚本开发之 Lua语言基础

热门文章

  1. 实验1 C语言开发环境...
  2. SqlSession 内部运行
  3. 百度地图api设置点的自定义图标不显示
  4. 安卓中如何调用jni
  5. (转载)STL map与Boost unordered_map的比较
  6. Java IO(2)
  7. Action 分离
  8. [Linux] 022 RPM 包查询
  9. 微信小程序(一)--微信小程序的介绍
  10. Manacher(最长镜面回文串)