C# winform中读写ini文件

INI文件是一种具有特定结构的文本文件,它的构成分为三部分,结构如下:

[Section1]
key 
1 = value2
key 
1 = value2
……
[Section2]
key 
1 = value1
key 
2 = 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函数的互操作。

读操作:

[csharp]  view plain  copy
  1. [DllImport("kernel32")]  
  2. private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);   
  3. section:要读取的段落名  
  4. key: 要读取的键  
  5. defVal: 读取异常的情况下的缺省值  
  6. retVal: key所对应的值,如果该key不存在则返回空值  
  7. size: 值允许的大小  
  8. filePath: INI文件的完整路径和文件名  
写操作:

[csharp]  view plain  copy
  1. [DllImport("kernel32")]   
  2. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);   
  3. section: 要写入的段落名  
  4. key: 要写入的键,如果该key存在则覆盖写入  
  5. val: key所对应的值  
  6. filePath: INI文件的完整路径和文件名  
这样,在就可以使用对他们的调用,用常规的方式定义一个名为 IniFile 类:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Runtime.InteropServices;  
  6. using System.IO;  
  7.   
  8. namespace 测试  
  9. {  
  10.     ///   
  11.     /// INI文件的操作类  
  12.     ///   
  13.     public class IniFile  
  14.     {  
  15.         public string Path;  
  16.   
  17.         public IniFile(string path)  
  18.         {  
  19.             this.Path = path;  
  20.         }  
  21.  
  22.         #region 声明读写INI文件的API函数  
  23.   
  24.         [DllImport("kernel32")]  
  25.         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);  
  26.   
  27.         [DllImport("kernel32")]  
  28.         private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);  
  29.   
  30.         [DllImport("kernel32")]  
  31.         private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);  
  32.  
  33.         #endregion  
  34.   
  35.         ///   
  36.         /// 写INI文件  
  37.         ///   
  38.         /// 段落  
  39.         ///   
  40.         ///   
  41.         public void IniWriteValue(string section, string key, string iValue)  
  42.         {  
  43.             WritePrivateProfileString(section, key, iValue, this.Path);  
  44.         }  
  45.   
  46.         ///   
  47.         /// 读取INI文件  
  48.         ///   
  49.         /// 段落  
  50.         ///   
  51.         /// 返回的键值  
  52.         public string IniReadValue(string section, string key)  
  53.         {  
  54.             StringBuilder temp = new StringBuilder(255);  
  55.   
  56.             int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);  
  57.             return temp.ToString();  
  58.         }  
  59.   
  60.         ///   
  61.         /// 读取INI文件  
  62.         ///   
  63.         /// 段,格式[]  
  64.         ///   
  65.         /// 返回byte类型的section组或键值组  
  66.         public byte[] IniReadValues(string section, string key)  
  67.         {  
  68.             byte[] temp = new byte[255];  
  69.   
  70.             int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);  
  71.             return temp;  
  72.         }  
  73.     }  
  74. }  

程序在调用IniFile类时需要先实例化

[csharp]  view plain  copy
  1. string path = Environment.CurrentDirectory + @"\Config.ini";  //指定ini文件的路径  
  2. IniFile ini = new IniFile(path);  
  3. string FpOrderNumber = ini.IniReadValue("SQL""FpOrderNumber");//读取ini文件中键FpOrderNumber的值ini.IniWriteValue("SQL", "FpOrderNumber", tsslMo.Text);//将tsslMo.Text的值写到键FpOrderNumber中去  




你可能感兴趣的:(C#)