此类实现对Ini文件的简单封装
可以提取文件中所有Sections 及Section中所对应的各个项
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
namespace TestIniFile
{
/// <summary>
/// IniFile 的摘要说明。
/// </summary>
public class IniFile
{
[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);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,byte[] retVal,int size,string filePath);
public IniFile()
{
}
public IniFile(string fileName)
{
iniFileName = fileName;
sectionArray = new ArrayList();
keyArray = new ArrayList();
}
//读取所有Sections并返回
public void ReadSections()
{
byte[] byteBuffer = new byte[11024];//设定Buffer长度
string tmpStr = null;
if (GetPrivateProfileString(null, null, null, byteBuffer, 1024, iniFileName) > 0)
{
foreach(byte abyte in byteBuffer)
{
if (abyte == 0)
{
if (tmpStr == null)
{
break;
}
else
{
sectionArray.Add(tmpStr);
tmpStr = null;
}
}
else
{
tmpStr = tmpStr + Convert.ToChar(abyte);
}
}
}
}
//读取某Section中Key的值的列表
public void ReadSectionKeys(string sectionName)
{
keyArray.Clear();
byte[] byteBuffer = new byte[11024];//设定Buffer长度
string tmpStr = null;
if (GetPrivateProfileString(sectionName, null, null, byteBuffer, 1024, iniFileName) > 0)
{
foreach(byte abyte in byteBuffer)
{
if (abyte == 0)
{
if (tmpStr == null)
{
break;
}
else
{
keyArray.Add(tmpStr);
tmpStr = null;
}
}
else
{
tmpStr = tmpStr + Convert.ToChar(abyte);
}
}
}
}
//判定Section是否存在
public bool SectionExists(string sectionName)
{
if (sectionArray.Count <= 0)
{
ReadSections();
}
foreach(object sectionList in sectionArray)
{
if ((string)sectionList == sectionName)
{
return true;
}
}
return false;
}
//判定Section中Key是否存在
public bool KeyExists(string sectionName, string keyName)
{
if (sectionArray.Count <= 0)
{
ReadSections();
}
foreach(object sectionList in sectionArray)
{
if ((string)sectionList == sectionName)
{
foreach(object keyList in keyArray)
{
if ((string)keyList == keyName)
{
return true;
}
}
}
}
return false;
}
//返回Section列表
public ArrayList GetSectionValues()
{
if (sectionArray.Count <=0 )
{
ReadSections();
}
return (ArrayList)sectionArray.Clone();
}
//返回Section中Key的列表
public ArrayList GetKeyValues(string sectionName)
{
ReadSectionKeys(sectionName);
return (ArrayList)keyArray.Clone();
}
//读入字符串型数据
public string ReadString(string sectionName, string keyName, string defaultVal)
{
StringBuilder retStr = new StringBuilder(1024);
if (GetPrivateProfileString(sectionName, keyName, defaultVal, retStr, 1024, iniFileName) != 0)
{
return retStr.ToString();
}
else
{
return defaultVal;
}
}
//写入字符串型数据
public bool WriteString(string sectionName, string keyName, string setVal)
{
if (WritePrivateProfileString(sectionName, keyName,setVal, iniFileName) != 0)
{
return true;
}
else
{
return false;
}
}
//读取数字型数据
public int ReadInteger(string sectionName, string keyName, int defaultVal)
{
return Convert.ToInt32(ReadString(sectionName, keyName, Convert.ToString(defaultVal)));
}
//写入数字型数据
public bool WriteInteger(string sectionName, string keyName, int setVal)
{
return WriteString(sectionName, keyName, Convert.ToString(setVal)) ;
}
//读取bool型数据
public bool ReadBool(string sectionName, string keyName, bool defaultVal)
{
return Convert.ToBoolean(ReadString(sectionName, keyName, Convert.ToString(defaultVal)));
}
//写入bool型数据
public bool WriteBool(string sectionName, string keyName, bool setVal)
{
return WriteString(sectionName, keyName, Convert.ToString(setVal)) ;
}
//读取Double型数据
public double ReadDouble(string sectionName, string keyName, double defaultVal)
{
return Convert.ToDouble(ReadString(sectionName, keyName, Convert.ToString(defaultVal)));
}
//写入Float型数据
public bool WriteDouble(string sectionName, string keyName, double setVal)
{
return WriteString(sectionName, keyName, Convert.ToString(setVal)) ;
}
//读取Datetime型数据
public DateTime ReadDatetime(string sectionName, string keyName, DateTime defaultVal)
{
return Convert.ToDateTime(ReadString(sectionName, keyName, Convert.ToString(defaultVal)));
}
//写入Datetime型数据
public bool WriteDatetime(string sectionName, string keyName, DateTime setVal)
{
return WriteString(sectionName, keyName, Convert.ToString(setVal)) ;
}
//删除所有Section
public bool DeleteSection(string sectionName)
{
if (WritePrivateProfileString(sectionName, null, null, iniFileName) != 0)
{
return true;
}
else
{
return false;
}
}
//删除所有Section中的Key
public bool DeleteKey(string sectionName, string KeyName)
{
if (WritePrivateProfileString(sectionName, KeyName, null, iniFileName) != 0)
{
return true;
}
else
{
return false;
}
}
//override tostring
public override string ToString()
{
return "This is IniFile Class";
}
private string iniFileName;
private ArrayList sectionArray;
private ArrayList keyArray;
}
}
本文地址:http://com.8s8s.com/it/it43307.htm