设为首页收藏本站language 语言切换
查看: 1534|回复: 0
收起左侧

[c#]:如何在C#中读写INI文件

[复制链接]
发表于 2010-2-25 10:25:00 | 显示全部楼层 |阅读模式
<p >INI文件就是扩展名为“ini”的文件。在Windows系统中,INI文件是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。但在某些场合,INI文件还拥有其不可替代的地位。本文就来探讨一下C#是如何对INI进行读写操作。<p ><center><font color="#000099"><strong>INI文件的结构</strong></font></center><p >INI文件是一种按照特点方式排列的文本文件。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>[Section1]  KeyWord1 = Valuel  KeyWord2 = Value2   ……  [Section2]  KeyWord3 = Value3  KeyWord4 = Value4</ccid_code></pre></td></tr></table></ccid_nobr><p >本文中介绍的程序设计及运行环境:<p >● 微软视窗2000 高级服务器版<p >● .Net Framework SDK正式版<p ><center><font color="#000099"><strong>C#和Win32 API函数</strong></font></center><p >C#并不像C++,拥有属于自己的类库。C#使用的类库是.Net框架为所有.Net程序开发提供的一个共有的类库——.Net FrameWork SDK。虽然.Net FrameWork SDK内容十分庞大,功能也非常强大,但还不能面面俱到,至少它并没有提供直接操作INI文件所需要的相关的类。在本文中,C#操作INI文件使用的是Windows系统自带Win32的API函数——WritePrivateProfileString()和GetPrivateProfileString()函数。这二个函数都位于“kernel32.dll”文件中。<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 ><b>C#申明INI文件的写操作函数WritePrivateProfileString():</b><p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>[ DllImport ( &quot;kernel32&quot; ) ]                private static extern long WritePrivateProfileString ( string section ,                        string key , string val , string filePath ) ;</ccid_code></pre></td></tr></table></ccid_nobr><p >参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。<p ><b>C#申明INI文件的读操作函数GetPrivateProfileString():</b><p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>[ DllImport ( &quot;kernel32&quot; ) ]        private static extern int GetPrivateProfileString ( string section ,                string key , string def , StringBuilder retVal ,                int size , string filePath ) ;</ccid_code></pre></td></tr></table></ccid_nobr><p >参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。<p ><center><font color="#000099"><strong>C#中读写INI文件的关键步骤和解决方法</strong></font></center><p ><b>C#对INI文件进行写操作:</b><p >对INI文件进行写操作,是通过组件button2的"Click"事件来实现的。这里有一点应该注意,当在调用WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息。下面是button2组件的"Click"事件对应的代码清单:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>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  ( &quot;成功写入INI文件!&quot; , &quot;信息&quot; ) ;                }</ccid_code></pre></td></tr></table></ccid_nobr><p ><b>C#对INI文件进行读操作:</b><p >正确读取INI的必须满足三个前提:INI文件的全路径、段落名称和关键字名称。否则就无法正确读取。你可以设定读取不成功后的缺省数值,在下面的程序中,为了直观设定的是“无法读取对应数值!”字符串,读取INI文件是通过button3组件的“Click”事件来实现的,下面是其对应的代码清单:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>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 ,&quot;无法读取对应数值!&quot;,                temp , 255 , FileName ) ;                        //显示读取的数值                        textBox4.Text = temp.ToString  ( ) ;                }</ccid_code></pre></td></tr></table></ccid_nobr><p ><center><font color="#000099"><strong>C#操作INI文件的完整源代码(ini.cs)和运行界面</strong></font></center><p >通过上面的这些介绍,我们不难得到用C#操作INI文件的完整代码清单(ini.cs),具体如下:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>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 ( &quot;kernel32&quot; ) ]                private static extern long WritePrivateProfileString ( string section ,                        string key , string val , string filePath ) ;                [ DllImport ( &quot;kernel32&quot; ) ]                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 ( 238 , 20 ) ;        this.button1.Name = &quot;button1&quot; ;        this.button1.Size = new System.Drawing.Size ( 100 , 32 ) ;        this.button1.TabIndex = 0 ;        this.button1.Text = &quot;选择INI文件&quot; ;        this.button1.Click += new System.EventHandler ( this.button1_Click ) ;        this.textBox1.Location = new System.Drawing.Point ( 58 , 22 ) ;        this.textBox1.Name = &quot;textBox1&quot; ;        this.textBox1.Size = new System.Drawing.Size ( 162 , 21 ) ;        this.textBox1.TabIndex = 1 ;        this.textBox1.Text = &quot;&quot; ;        this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;        this.button2.Location = new System.Drawing.Point ( 86 , 168 ) ;        this.button2.Name = &quot;button2&quot; ;        this.button2.Size = new System.Drawing.Size ( 98 , 30 ) ;        this.button2.TabIndex = 3 ;        this.button2.Text = &quot;写入INI文件&quot; ;        this.button2.Click += new System.EventHandler ( this.button2_Click ) ;        this.textBox2.Location = new System.Drawing.Point ( 160 , 62 ) ;        this.textBox2.Name = &quot;textBox2&quot; ;        this.textBox2.Size = new System.Drawing.Size ( 176 , 21 ) ;        this.textBox2.TabIndex = 5 ;        this.textBox2.Text = &quot;&quot; ;        this.textBox3.Location = new System.Drawing.Point ( 160 , 94 ) ;        this.textBox3.Name = &quot;textBox3&quot; ;        this.textBox3.Size = new System.Drawing.Size ( 176 , 21 ) ;        this.textBox3.TabIndex = 6 ;        this.textBox3.Text = &quot;&quot; ;        this.textBox4.Location = new System.Drawing.Point ( 160 , 128 ) ;        this.textBox4.Name = &quot;textBox4&quot; ;        this.textBox4.Size = new System.Drawing.Size ( 176 , 21 ) ;        this.textBox4.TabIndex = 7 ;        this.textBox4.Text = &quot;&quot; ;        this.label1.Location = new System.Drawing.Point ( 56 , 62 ) ;        this.label1.Name = &quot;label1&quot; ;        this.label1.TabIndex = 8 ;        this.label1.Text = &quot;段落名称:&quot; ;        this.label2.Location = new System.Drawing.Point ( 66 , 96 ) ;        this.label2.Name = &quot;label2&quot; ;        this.label2.TabIndex = 9 ;        this.label2.Text = &quot;关键字:&quot; ;        this.label3.Location = new System.Drawing.Point ( 42 , 130 ) ;        this.label3.Name = &quot;label3&quot; ;        this.label3.TabIndex = 10 ;        this.label3.Text = &quot;关键字数值:&quot; ;        this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;        this.button3.Location = new System.Drawing.Point ( 208 , 168 ) ;        this.button3.Name = &quot;button3&quot; ;        this.button3.Size = new System.Drawing.Size ( 98 , 30 ) ;        this.button3.TabIndex = 11 ;        this.button3.Text = &quot;读取INI数值&quot; ;        this.button3.Click += new System.EventHandler ( this.button3_Click ) ;        this.openFileDialog1.Filter = &quot;INI 文件|*.ini&quot; ;        this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;        this.ClientSize = new System.Drawing.Size ( 366 , 217 ) ;        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 = &quot;Form1&quot; ;        this.Text = &quot;C#操作INI文件--写操作&quot; ;        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  ( &quot;成功写入INI文件!&quot; , &quot;信息&quot; ) ;        }        //读取指定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 ,                &quot;无法读取对应数值!&quot; , emp , 255 , FileName ) ;                //显示读取的数值                        textBox4.Text = temp.ToString  ( ) ;                }        }}</ccid_code></pre></td></tr></table></ccid_nobr><p >下图是ini.cs编译后的运行界面:<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_vzzthh34641.jpg"></center><p ><center>图1:C#操作INI文件的运行界面</center><p ><center><font color="#000099"><strong>总结</strong></font></center><p >通过上面的这些介绍,可以看成C#操作INI文件的过程,其实就是C#调用Win32的API函数的过程。掌握了如何在C#申明Win32的API函数,再来操作INI就显得非常简单。<p >(责任编辑 <ccid_nobr>Sunny</ccid_nobr>)                                 <p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-10 15:14 , Processed in 0.106302 second(s), 22 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表