在这个漫天都是XML配置文件飞的年代很难在找我们这些还喜欢用INI/INF/CFG配置文件的人了
有时候真觉得让人感伤XML的性能是无法比拟INI配置文件的 但XML的树形结构直观可析 不过其
实INI配置文件同样可以建立为树形结构本身它便是树形结构 有人可能说INI不安全 那么我请问你
XML又安全在哪里了呢 还不是一个鬼样 XML除结构清晰层次分明 但在性能上它与INI比不占优势
推崇它可以 但别把它和INI结构配置文件进行比较 它们本身就是不一样的东西
下面的代码按照.Net的风格编写的一套INI配置文件类 想必大家应该可以轻松上手 函数名你会发
现会有些XmlDocument的命名然而它们的用法本质都一样 Load加载 / Save 保存
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Configuration
{
internal class Win32Native
{
[DllImport("Kernel32.dll", EntryPoint = "GetPrivateProfileSectionNamesA")]
public static extern int GetPrivateProfileSectionNames([Out, MarshalAs(UnmanagedType.LPArray)] char[] lpszReturnBuffer, int nSize, [In, MarshalAs(UnmanagedType.LPStr)] string lpFileName);
[DllImport("Kernel32.dll", EntryPoint = "GetPrivateProfileSectionA")]
public static extern int GetPrivateProfileSection(string lpAppName, [MarshalAs(UnmanagedType.VBByRefStr)]ref string lpReturnedString, int nSize, string lpFileName);
[DllImport("Kernel32.dll", EntryPoint = "WritePrivateProfileStringA")]
public static extern int WritePrivateProfileString(string section, string key, string value, string files);
}
public class Profile // ini
{
public string FileName
{
get;
set;
}
public Profile()
{
this.Sections = new SectionCollection(this);
}
public Profile(string filename) : this()
{
this.FileName = filename;
}
public void Load()
{
int len; char[] buffer;
if ((len = this.Length) > 0)