INI文件是一种常用的存储应用程序配置的文件,在早期的应用程序中有大量的应用,目前已经逐渐被XML文件所替代。但在Windows操作系统中还存在大量的INI配置文件,一些应用程序依然使用INI文件作为参数的保存手段。如Windows目录下ODBC.INI文件,保存了ODBC配置。INI配置文件保存的是普通文本,使用起来比较方便。
实例,在应用程序中使用INI文件:
添加如下引用:
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace IniDemo
{
///<summary>
///Inifile类
///</summary>
public class Inifile
{
public string path;
///<summary>
///导入系统中写入INI文件的函数
///</summary>
///<param name="section">段</param>
///<param name="key">键</param>
///<param name="val">值</param>
///<param name="filepath">路径</param>
///<returns></returns>
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filepath);
///<summary>
///导入系统中读取INI文件的函数
///</summary>
///<param name="section">段</param>
///<param name="key">键</param>
///<param name="def">定义</param>
///<param name="val">值</param>
///<param name="size">大小</param>
///<param name="filepath">路径</param>
///<returns></returns>
[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 IniFile(string INIPath)
{
path=INIPath;
}
///<summary>
///写入数据
///</summary>
///<PARAM name="Section"></PARAM>
///段名
///<PARAM name="Key"></PARAM>
///键
///<PARAM name="Value"></PARAM>
///值
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
///<summary>
///读取INI文件
///</summary>
///<PARAM name="Section">段</PARAM>
///<PARAM name="Key">键</PARAM>
///<PARAM name="Value">值</PARAM>
///<returns></returns>
public string IniReadValue(string Section,string Key)
{
StringBuilder temp=new StringBuilder(255);
int i=GetPrivateProfileString(Section,Key," ",temp,255,this.path);
return temp.ToString();
}
}
}
在Program.cs中写入以下代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace IniDemo
{
class Program
{
static void Main(string[ ] args)
{
try
{
IniFile iniFile=new IniFile(@ "C:\test.ini");
Console.WriteLine("创建成功!");
}
catch(Exception e)
{
Console.WriteLine("创建失败!");
}
}
}
}
运行后,C盘根目录出现test.ini文件,文件内容为:
[test]
test=test
继续改写Program.cs文件代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace IniDemo
{
class Program
{
static void Main(string[ ] args)
{
try
{
IniFile iniFile=new IniFile(@ "C:\test.ini");
iniFile.IniWriteValue("section1","key1","value1");
iniFile.IniWriteValue("section2","key2","value2");
iniFile.IniWriteValue("section3","key3","value3");
Console.WriteLine("创建成功!");
}
catch(Exception e)
{
Console.WriteLine("创建失败!");
}
}
}
}
运行后INI文件内容为:[section1]
key1=value1
[section2]
key2=value2
[section3]
key3=value3
可以通过该类任意添加INI文件的内容
查看文件内容代码如下:
IniFile iniFile=new IniFile(@ "C:\test.ini");
string value1=iniFile.IniReadValue("section1","key1");
string value2=iniFile.IniReadValue("section2","key2");
string value3=iniFile.IniReadValue("section3","key3");
Console.WriteLine(iniFile.path);
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);
Console.WriteLine("读取成功!");
Console.ReadLine();