using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace INIFILE { public abstract class CustomIniFile { public CustomIniFile(string AFileName) { FFileName = AFileName; } private string FFileName; public string FileName { get { return FFileName; } } public virtual bool SectionExists(string Section) { List vStrings = new List(); ReadSections(vStrings); return vStrings.Contains(Section); } public virtual bool ValueExists(string Section, string Ident) { List vStrings = new List(); ReadSection(Section, vStrings); return vStrings.Contains(Ident); } public abstract string ReadString(string Section, string Ident, string Default); public abstract bool WriteString(string Section, string Ident, string Value); public abstract bool ReadSectionValues(string Section, List Strings); public abstract bool ReadSection(string Section, List Strings); public abstract bool ReadSections(List Strings); public abstract bool EraseSection(string Section); public abstract bool DeleteKey(string Section, string Ident); public abstract bool UpdateFile(); } /// /// 存储本地INI文件的类。 /// public class IniFile : CustomIniFile { [DllImport("kernel32")] private static extern uint GetPrivateProfileString( string lpAppName, // points to section name string lpKeyName, // points to key name string lpDefault, // points to default string byte[] lpReturnedString, // points to destination buffer uint nSize, // size of destination buffer string lpFileName // points to initialization filename ); [DllImport("kernel32")] private static extern bool WritePrivateProfileString( string lpAppName, // pointer to section name string lpKeyName, // pointer to key name string lpString, // pointer to string to add string lpFileName // pointer to initialization filename ); /// /// 构造IniFile实例。 /// 指定文件名 /// public IniFile(string AFileName) : base(AFileName) { } /// /// 析够IniFile实例。 /// ~IniFile() { UpdateFile(); } /// /// 读取字符串值。 /// 指定变量标识。 /// 指定所在区域。 /// 指定默认值。 /// 返回读取的字符串。如果读取失败则返回该值。 /// public override string ReadString(string Section, string Ident, string Default) { byte[] vBuffer = new byte[2048]; uint vCount = GetPrivateProfileString(Section, Ident, Default, vBuffer, (uint)vBuffer.Length, FileName); return Encoding.Default.GetString(vBuffer, 0, (int)vCount); } /// /// 写入字符串值。 /// /// 指定所在区域。 /// 指定变量标识。 /// 所要写入的变量值。 /// 返回写入是否成功。 public override bool WriteString(string Section, string Ident, string Value) { return WritePrivateProfileString(Section, Ident, Value, FileName); } /// /// 获得区域的完整文本。(变量名=值格式)。 /// /// 指定区域标识。 /// 输出处理结果。 /// 返回读取是否成功。 public override bool ReadSectionValues(string Section, List Strings) { List vIdentList = new List(); if (!ReadSection(Section, vIdentList)) return false; foreach (string vIdent in vIdentList) Strings.Add(string.Format("{0}={1}", vIdent, ReadString(Section, vIdent, ""))); return true; } /// /// 读取区域变量名列表。 /// /// 指定区域名。 /// 指定输出列表。 /// 返回获取是否成功。 public override bool ReadSection(string Section, List Strings) { byte[] vBuffer = new byte[16384]; uint vLength = GetPrivateProfileString(Section, null, null, vBuffer, (uint)vBuffer.Length, FileName); int j = 0; for (int i = 0; i < vLength; i++) if (vBuffer[i] == 0) { Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j)); j = i + 1; } return true; } /// /// 读取区域名列表。 /// /// 指定输出列表。 /// public override bool ReadSections(List Strings) { byte[] vBuffer = new byte[16384]; uint vLength = GetPrivateProfileString(null, null, null, vBuffer, (uint)vBuffer.Length, FileName); int j = 0; for (int i = 0; i < vLength; i++) if (vBuffer[i] == 0) { Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j)); j = i + 1; } return true; } /// /// 删除指定区域。 /// /// 指定区域名。 /// 返回删除是否成功。 public override bool EraseSection(string Section) { return WritePrivateProfileString(Section, null, null, FileName); } /// /// 删除指定变量。 /// /// 变量所在区域。 /// 变量标识。 /// 返回删除是否成功。 public override bool DeleteKey(string Section, string Ident) { return WritePrivateProfileString(Section, Ident, null, FileName); } /// /// 更新文件。 /// /// 返回更新是否成功。 public override bool UpdateFile() { return WritePrivateProfileString(null, null, null, FileName); } } }