ini文件其实就是一个文本文件,它有固定的格式,节Section的名字用[]括起来,然后换行说明key的值:
[section]
key=value
如数据库服务器配置文件:
DBServer.ini
[Server]
Name=localhost
[DB]
Name=NorthWind
[User]
Name=sa
在c#中,对配置文件的读写是通过API函数来实现的:
using System;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace PubOp
{
public class OperateIniFile
{
#region API函数声明
[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section,string key,
string val,string filePath);
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section,string key,
string def,StringBuilder retVal,int size,string filePath);
#endregion
#region 读Ini文件
public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
return temp.ToString();
}
else
{
return String.Empty;
}
}
#endregion
#region 写Ini文件
public static bool WriteIniData(string Section,string Key,string Value,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);
if(OpStation == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
#endregion
}
}