INI文件就是扩展名为“ini”的文件。在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。但在某些场合,INI文件还拥有其不可替代的地位。本文就来探讨一下C#是如何对INI进行读写操作。 </p>
<p>INI文件的结构 </p>
<p>INI文件是一种按照特点方式排列的文本文件。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下: </p>
<p>[Section1] </p>
<p>  KeyWord1 = Valuel </p>
<p>  KeyWord2 = Value2 </p>
<p>   …… </p>
<p>  [Section2] </p>
<p>  KeyWord3 = Value3 </p>
<p>  KeyWord4 = Value4 </p>
<p>本文中介绍的程序设计及运行环境: </p>
<p>● 微软视窗2000 高级服务器版 </p>
<p>● .Net Framework SDK正式版 </p>
<p>C#和Win32 API函数 </p>
<p>C#并不像C++,拥有属于自己的类库。C#使用的类库是.Net框架为所有.Net程序开发提供的一个共有的类库——.Net FrameWork SDK。虽然.Net FrameWork SDK内容十分庞大,功能也非常强大,但还不能面面俱到,至少它并没有提供直接操作INI文件所需要的相关的类。在本文中,C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()和GetPrivateProfileString()函数。这二个函数都位于“kernel32.dll”文件中。 </p>
<p>我们知道在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函数: </p>
<p>C#申明INI文件的写操作函数WritePrivateProfileString(): </p>
<p>         [ DllImport ( "kernel32" ) ] </p>
<p>         private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ; </p>
<p>参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。 </p>
<p>C#申明INI文件的读操作函数GetPrivateProfileString(): </p>
<p>         [ DllImport ( "kernel32" ) ] </p>
<p>         private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal , int size , string filePath ) ; </p>
<p>参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。 </p>
<p>C#中读写INI文件的关键步骤和解决方法 </p>
<p>C#对INI文件进行写操作: </p>
<p>         //写入INI文件 </p>
<p>         private void button2_Click ( object sender , System.EventArgs e ) </p>
<p>         { </p>
<p>              string FileName = textBox1.Text ; </p>
<p>              string section = textBox2.Text ; </p>
<p>              string key = textBox3.Text ; </p>
<p>              string keyValue = textBox4.Text ; </p>
<p>              WritePrivateProfileString ( section , key , keyValue , FileName ) ; </p>
<p>              MessageBox.Show( "成功写入INI文件!" , "信息" ) ; </p>
<p>         } </p>
<p>对INI文件进行写操作,是通过组件button2的"Click"事件来实现的。这里有一点应该注意,当在调用WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息。下面是button2组件的"Click"事件对应的代码清单: </p>
<p>C#对INI文件进行读操作:</p>
<p>正确读取INI的必须满足三个前提:INI文件的全路径、段落名称和关键字名称。否则就无法正确读取。你可以设定读取不成功后的缺省数值,在下面的程序中,为了直观设定的是“无法读取对应数值!”字符串,读取INI文件是通过button3组件的“Click”事件来实现的,下面是其对应的代码清单: </p>
<p>         //读取指定INI文件的特定段落中的关键字的数值 </p>
<p>         private void button3_Click ( object sender , System.EventArgs e ) </p>
<p>         { </p>
<p>              StringBuilder temp = new StringBuilder ( 255 ) ; </p>
<p>              string FileName = textBox1.Text ; </p>
<p>              string section = textBox2.Text ; </p>
<p>              string key = textBox3.Text ; </p>
<p>              int i = GetPrivateProfileString ( section , key , </p>
<p>                   "无法读取对应数值!" , temp , 255 , FileName ) ; </p>
<p>              //显示读取的数值 </p>
<p>              textBox4.Text = temp.ToString(); </p>
<p>         }</p>
<p>C#操作INI文件的完整源代码(ini.cs)和运行界面 </p>
<p>通过上面的这些介绍,我们不难得到用C#操作INI文件的完整代码清单(ini.cs),具体如下:</p>
<p>using System ; </p>
<p>using System.Drawing ; </p>
<p>using System.Collections ; </p>
<p>using System.ComponentModel ; </p>
<p>using System.Windows.Forms ; </p>
<p>using System.Data ; </p>
<p>using System.Runtime.InteropServices ; </p>
<p>using System.Text ; </p>
<p>namespace C_ReadIni </p>
<p>{ </p>
<p>     public class Form1 : System.Windows.Forms.Form </p>
<p>     { </p>
<p>         private System.Windows.Forms.Button button1 ; </p>
<p>         private System.Windows.Forms.TextBox textBox1 ; </p>
<p>         private System.Windows.Forms.Button button2 ; </p>
<p>         private System.Windows.Forms.TextBox textBox2 ; </p>
<p>         private System.Windows.Forms.TextBox textBox3 ; </p>
<p>         private System.Windows.Forms.TextBox textBox4 ; </p>
<p>         private System.Windows.Forms.Label label1 ; </p>
<p>         private System.Windows.Forms.Label label2 ; </p>
<p>         private System.Windows.Forms.Label label3 ; </p>
<p>         private System.Windows.Forms.Button button3 ; </p>
<p>         private System.Windows.Forms.OpenFileDialog openFileDialog1 ; </p>
<p>         private System.ComponentModel.Container components = null ; </p>
<p>         public Form1 ( ) </p>
<p>         { </p>
<p>              InitializeComponent ( ) ; </p>
<p>         } </p>
<p>         protected override void Dispose (bool disposing) </p>
<p>         { </p>
<p>              if (disposing) </p>
<p>              { </p>
<p>                   if(components != null) </p>
<p>                   { </p>
<p>                       components.Dispose ( ) ; </p>
<p>                   } </p>
<p>              } </p>
<p>              base.Dispose (disposing); </p>
<p>         } </p>
<p>         [ DllImport ( "kernel32" ) ] </p>
<p>         private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ; </p>
<p>         [ DllImport ( "kernel32" ) ] </p>
<p>         private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal , </p>
<p>              int size , string filePath ) ; </p>
<p>         private void InitializeComponent ( ) </p>
<p>         { </p>
<p>              this.button1 = new System.Windows.Forms.Button ( ) ; </p>
<p>              this.textBox1 = new System.Windows.Forms.TextBox ( ) ; </p>
<p>              this.button2 = new System.Windows.Forms.Button ( ) ; </p>
<p>              this.textBox2 = new System.Windows.Forms.TextBox ( ) ; </p>
<p>              this.textBox3 = new System.Windows.Forms.TextBox ( ) ; </p>
<p>              this.textBox4 = new System.Windows.Forms.TextBox ( ) ; </p>
<p>              this.label1 = new System.Windows.Forms.Label ( ) ; </p>
<p>              this.label2 = new System.Windows.Forms.Label ( ) ; </p>
<p>              this.label3 = new System.Windows.Forms.Label ( ) ; </p>
<p>              this.button3 = new System.Windows.Forms.Button ( ) ; </p>
<p>              this.openFileDialog1 = new </p>
<p>                   System.Windows.Forms.OpenFileDialog ( ) ; </p>
<p>              this.SuspendLayout ( ) ; </p>
<p>              this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; </p>
<p>              this.button1.Location = new System.Drawing.Point ( 238 , 20 ) ; </p>
<p>              this.button1.Name = "button1" ; </p>
<p>              this.button1.Size = new System.Drawing.Size ( 100 , 32 ) ; </p>
<p>              this.button1.TabIndex = 0 ; </p>
<p>              this.button1.Text = "选择INI文件" ; </p>
<p>              this.button1.Click += new System.EventHandler ( this.button1_Click ) ; </p>
<p>              this.textBox1.Location = new System.Drawing.Point ( 58 , 22 ) ; </p>
<p>              this.textBox1.Name = "textBox1" ; </p>
<p>              this.textBox1.Size = new System.Drawing.Size ( 162 , 21 ) ; </p>
<p>              this.textBox1.TabIndex = 1 ; </p>
<p>              this.textBox1.Text = "" ; </p>
<p>              this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; </p>
<p>              this.button2.Location = new System.Drawing.Point ( 86 , 168 ) ; </p>
<p>              this.button2.Name = "button2" ; </p>
<p>              this.button2.Size = new System.Drawing.Size ( 98 , 30 ) ; </p>
<p>              this.button2.TabIndex = 3 ; </p>
<p>              this.button2.Text = "写入INI文件" ; </p>
<p>              this.button2.Click += new System.EventHandler ( this.button2_Click ) ; </p>
<p>              this.textBox2.Location = new System.Drawing.Point ( 160 , 62 ) ; </p>
<p>              this.textBox2.Name = "textBox2" ; </p>
<p>              this.textBox2.Size = new System.Drawing.Size ( 176 , 21 ) ; </p>
<p>              this.textBox2.TabIndex = 5 ; </p>
<p>              this.textBox2.Text = "" ; </p>
<p>              this.textBox3.Location = new System.Drawing.Point ( 160 , 94 ) ; </p>
<p>              this.textBox3.Name = "textBox3" ; </p>
<p>              this.textBox3.Size = new System.Drawing.Size ( 176 , 21 ) ; </p>
<p>              this.textBox3.TabIndex = 6 ; </p>
<p>              this.textBox3.Text = "" ; </p>
<p>              this.textBox4.Location = new System.Drawing.Point ( 160 , 128 ) ; </p>
<p>              this.textBox4.Name = "textBox4" ; </p>
<p>              this.textBox4.Size = new System.Drawing.Size ( 176 , 21 ) ; </p>
<p>              this.textBox4.TabIndex = 7 ; </p>
<p>              this.textBox4.Text = "" ; </p>
<p>              this.label1.Location = new System.Drawing.Point ( 56 , 62 ) ; </p>
<p>              this.label1.Name = "label1" ; </p>
<p>              this.label1.TabIndex = 8 ; </p>
<p>              this.label1.Text = "段落名称:" ; </p>
<p>              this.label2.Location = new System.Drawing.Point ( 66 , 96 ) ; </p>
<p>              this.label2.Name = "label2" ; </p>
<p>              this.label2.TabIndex = 9 ; </p>
<p>              this.label2.Text = "关键字:" ; </p>
<p>              this.label3.Location = new System.Drawing.Point ( 42 , 130 ) ; </p>
<p>              this.label3.Name = "label3" ; </p>
<p>              this.label3.TabIndex = 10 ; </p>
<p>              this.label3.Text = "关键字数值:" ; </p>
<p>              this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; </p>
<p>              this.button3.Location = new System.Drawing.Point ( 208 , 168 ) ; </p>
<p>              this.button3.Name = "button3" ; </p>
<p>              this.button3.Size = new System.Drawing.Size ( 98 , 30 ) ; </p>
<p>              this.button3.TabIndex = 11 ; </p>
<p>              this.button3.Text = "读取INI数值" ; </p>
<p>              this.button3.Click += new System.EventHandler ( this.button3_Click ) ; </p>
<p>              this.openFileDialog1.Filter = "INI 文件|*.ini" ; </p>
<p>              this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; </p>
<p>              this.ClientSize = new System.Drawing.Size ( 366 , 217 ) ; </p>
<p>              this.Controls.AddRange ( new System.Windows.Forms.Control [ ] { </p>
<p>                                                                                           this.button3 , </p>
<p>                                                                                           this.textBox4 , </p>
<p>                                                                                           this.textBox3 , </p>
<p>                                                                                           this.textBox2 , </p>
<p>                                                                                           this.button2 , </p>
<p>                                                                                           this.textBox1 , </p>
<p>                                                                                           this.button1 , </p>
<p>                                                                                           this.label3 , </p>
<p>                                                                                           this.label2 , </p>
<p>                                                                                           this.label1 } ) ; </p>
<p>              this.MaximizeBox = false ; </p>
<p>              this.Name = "Form1" ; </p>
<p>              this.Text = "C#操作INI文件--写操作" ; </p>
<p>              this.ResumeLayout ( false ) ; </p>
<p>         } </p>
<p>         [STAThread] </p>
<p>         static void Main ( )</p>
<p>         { </p>
<p>              Application.Run ( new Form1 ( )) ; </p>
<p>         } </p>
<p>         private void button1_Click ( object sender , System.EventArgs e ) </p>
<p>         { </p>
<p>              openFileDialog1.ShowDialog ( ) ; </p>
<p>              textBox1.Text = openFileDialog1.FileName ; </p>
<p>         } </p>
<p>         //写入INI文件 </p>
<p>         private void button2_Click ( object sender , System.EventArgs e ) </p>
<p>         { </p>
<p>              string FileName = textBox1.Text ; </p>
<p>              string section = textBox2.Text ; </p>
<p>              string key = textBox3.Text ; </p>
<p>              string keyValue = textBox4.Text ; </p>
<p>              WritePrivateProfileString ( section , key , keyValue , FileName ) ; </p>
<p>              MessageBox.Show( "成功写入INI文件!" , "信息" ) ; </p>
<p>         } </p>
<p>         //读取指定INI文件的特定段落中的关键字的数值 </p>
<p>         private void button3_Click ( object sender , System.EventArgs e ) </p>
<p>         { </p>
<p>              StringBuilder temp = new StringBuilder ( 255 ) ; </p>
<p>              string FileName = textBox1.Text ; </p>
<p>              string section = textBox2.Text ; </p>
<p>              string key = textBox3.Text ; </p>
<p>              int i = GetPrivateProfileString ( section , key , </p>
<p>                   "无法读取对应数值!" , temp , 255 , FileName ) ; </p>
<p>              //显示读取的数值 </p>
<p>              textBox4.Text = temp.ToString(); </p>
<p>         } </p>
<p>     } </p>
<p>}<br />

INI文件,WritePrivateProfileString()和GetPrivateProfileString()函数----转载的更多相关文章

  1. VB读写INI文件的四个函数以及相关API详细说明

    WritePrivateProfileString函数说明  来源:http://blog.csdn.net/wjb9921/article/details/2005000 在我们写的程序当中,总有一 ...

  2. Windows文件操作的API函数[转载]

    在VC中,大多数情况对文件的操作都使用系统提供的 API 函数,但有的函数我们不是很熟悉,以下提供一些文件操作 API 函数介绍: 一般文件操作 API CreateFile 打开文件 要对文件进行读 ...

  3. PHP获取与操作php.ini文件的几个函数示例

    php有一套设置和获取配置信息的函数,用于设置与修改相关参数信息.   1.ini_get()获取配置参数,ini_set()设置配置参数 <?php 2.ini_get_all()获取所有配置 ...

  4. 关于php的ini文件相关操作函数浅析

    在小公司,特别是创业型公司,整个服务器的搭建一般也是我们 PHP 开发工程师的职责之一.其中,最主要的一项就是要配置好服务器的 php.ini 文件.一些参数会对服务器的性能产生深远的影响,而且也有些 ...

  5. C#读写ini文件操作

    ini文件,是windows操作系统下的配置文件,ini文件是一种按照特点方式排列的文本文件,它的构成分为三部分,结构如下: [Section1] key 1 = value2 key 1 = val ...

  6. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  7. c#读取INI文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. ini文件操作

    Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...

  9. c#读取INI文件类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;na ...

随机推荐

  1. 【MySQL】用户管理及备份

    "我们知道我们的最高权限管理者是root用户,它拥有着最高的权限,包括select.update.delete.grant等操作.一般在公司里DBA工程师会创建一个用户和密码,让你去连接数据 ...

  2. P3378 (模板)并查集

    使用带路径压缩的并查集,不然会TLE AC代码: #include <bits/stdc++.h> #define MP make_pair #define PB push_back #d ...

  3. 解决STM32工程出现:Undefined symbol TIM_ClearFlag (referred from hcsr04.o).错误。类型问题Undefined symbol TIM_xxx (referred from xxx.o).

    出错原因: 工程FWLIB目录下没有添加stm32f10x_tim.c文件. 添加即可. 一般利用库开发,将ppp.c(ppp.c又调用了库stm32f10x_xx.h)写好之后的调用步骤: 举例使用 ...

  4. Plastic Bottle Manufacturer - Different Cosmetic Plastic Bottle Materials, Different Characteristics

    Plastic bottles are usually made of PP, PE, K, AS, abs, acrylic, PET, and the like. Dust caps for th ...

  5. windows下如何快速删除大文件

    rmdir  磁盘:\文件夹的名字  /s /q; eg:rmdir E:\vue_workspace\KB\day08    /s/q /S 表示除目录本身外,还将删除指定目录下的所有子目录和文件. ...

  6. SSL 证书格式普及,PEM、CER、JKS、PKCS12

    根据不同的服务器以及服务器的版本,我们需要用到不同的证书格式,就市面上主流的服务器来说,大概有以下格式: .DER .CER,文件是二进制格式,只保存证书,不保存私钥. .PEM,一般是文本格式,可保 ...

  7. 洛谷P1144最短路计数

    题目描述 给出一个NNN个顶点MMM条边的无向无权图,顶点编号为1−N1-N1−N.问从顶点111开始,到其他每个点的最短路有几条. 输入格式 第一行包含222个正整数N,MN,MN,M,为图的顶点数 ...

  8. Codeforces Round #611 (Div. 3) C

    There are nn friends who want to give gifts for the New Year to each other. Each friend should give ...

  9. tf.app.run()的作用

    tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...

  10. ES Search API

    Search API 搜索请求 SearchRequest用于与搜索文档.聚合.suggestions相关的任何操作,还提供了在结果文档上请求高亮的方法. 在最基本的表单中,我们可以向请求添加查询: ...