zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C# 读写ini配置文件demo

c#配置文件 读写 Demo ini
2023-09-11 14:16:45 时间

INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数.
INI文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键字(KEYWORD)和一个等号,等号右边就是关键字的值(VALUE).例如:
[Section1]
    KeyWord1 = Value1
    KeyWord2 = Value2
    ...
[Section2]
    KeyWord3 = Value3
    KeyWord4 = Value4

C#命名空间中没有直接读写INI的类,当然如果你把INT当成文本文件用System.IO类来读写算我没说.
我现在介绍的是系统处理INI的方法.
虽然C#中没有,但是在"kernel32.dll"这个文件中有Win32的API函数--WritePrivateProfileString()和GetPrivateProfileString()
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文件的完整路径和名称。 

下面是一个读写INI文件的类:

public   class  INIClass
{
  public   string  inipath;
 [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);
  ///   <summary>
  ///  构造方法
  ///   </summary>
  ///   <param name="INIPath"> 文件路径 </param>
  public  INIClass( string  INIPath)
 {
  inipath  =  INIPath;
 }
  ///   <summary>
  ///  写入INI文件
  ///   </summary>
  ///   <param name="Section"> 项目名称(如 [TypeName] ) </param>
  ///   <param name="Key"> 键 </param>
  ///   <param name="Value"> 值 </param>
  public   void  IniWriteValue( string  Section, string  Key, string  Value)
 {
  WritePrivateProfileString(Section,Key,Value, this .inipath);
 }
  ///   <summary>
  ///  读出INI文件
  ///   </summary>
  ///   <param name="Section"> 项目名称(如 [TypeName] ) </param>
  ///   <param name="Key"> 键 </param>
  public   string  IniReadValue( string  Section, string  Key)
 {
  StringBuilder temp  =   new  StringBuilder( 500 );
   int  i  =  GetPrivateProfileString(Section,Key, "" ,temp, 500 , this .inipath);
   return  temp.ToString();
 }
  ///   <summary>
  ///  验证文件是否存在
  ///   </summary>
  ///   <returns> 布尔值 </returns>
  public   bool  ExistINIFile()
 {
   return  File.Exists(inipath);
 }
}

 

C# 读写ini配置文件
1、新建类,引入命名空间 using System.Runtime.InteropServices;
2、声明API函数:

[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);

3、实现:写

/// <summary> 
/// 写入INI文件 
/// </summary> 
/// <param name="field">项目名称(如 [TypeName] )</param> 
/// <param name="Key">键</param> 
/// <param name="Value">值</param> 
public void GetValueOfKey(string field, string Key, string Value)
{
	WritePrivateProfileString(field, Key, Value, iniFilePath);
}

4、实现:读

/// <summary> 
/// 读出INI文件 
/// </summary> 
/// <param name="field">项目名称(如 [TypeName] )</param> 
/// <param name="Key">键</param> 
public string IniReadValue(string field, string Key)
{
	StringBuilder temp = new StringBuilder(500);
	int i = GetPrivateProfileString(field, Key, "", temp, 500, this.inipath);
	return temp.ToString();
}