INI配置文件C#类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

在这个漫天都是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)  
            {  
                buffer = new char[len];  
                if ((len = Win32Native.GetPrivateProfileSectionNames(buffer, len, this.FileName)) > 0)  
                {  
                    unsafe  
                    {  
                        fixed (char* str = buffer)  
                        {  
                            char* ptr = str; int index = 0, last = 0;  
                            while (index < len)  
                            {  
                                if (*ptr++ == '\0')  
                                {  
                                    int size = index - last;  
                                    char[] name = new char[size];  
                                    Array.Copy(buffer, last, name, 0, size);  
                                    this.Sections.Add(name);  
                                    last = index + 1;  
                                }  
                                index++;  
                            }  
                        }  
                    }  
                }  
            }  
        }  
  
  
        public bool Create()  
        {  
  
            try  
            {  
                Stream stream = null;  
                try  
                {  
                    stream = File.Create(this.FileName);  
                }  
                finally  
                {  
                    if (stream != null)  
                        stream.Dispose();  
                }  
                return true;  
            }  
            catch  
            {  
                return false;  
            }  
        }  
  
        public void Save()  
        {  
            this.Delete(); this.Create(); int[] len = new int[2];  
            if ((len[0] = Sections.Count) > 0)  
                for (int i = 0; i < len[0]; i++)  
                {  
                    Section section = this.Sections[i];  
                    if (section != null && (len[1] = section.Keys.Count) > 0)  
                    {  
                        for (int j = 0; j < len[1]; j++)  
                        {  
                            Key key = section.Keys[j];  
                            if (section != null)  
                                Win32Native.WritePrivateProfileString(section.Name, key.Name, key.Value, this.FileName);  
  
                        }  
                    }  
                }  
        }  
  
  
        public bool Delete()  
        {  
  
            try  
            {  
                if (File.Exists(this.FileName))  
                    File.Delete(this.FileName);  
                return true;  
            }  
            catch  
            {  
                return false;  
            }  
        }  
  
  
        public SectionCollection Sections // app name.  
        {  
            get;  
            set;  
        }  
  
        public int Length  
        {  
            get  
            {  
                if (File.Exists(this.FileName))  
                {  
  
                    FileInfo f_info = new FileInfo(this.FileName);  
                    return (int)f_info.Length;  
                }  
                return 0;  
            }  
        }  
    }  
  
  
    public class SectionCollection  
    {  
        private ArrayList m_items = new ArrayList();  
  
        public Profile Profile  
        {  
            get;  
            set;  
        }  
  
        public Section Add(string name)  
        {  
            Section section = new Section(this.Profile);  
            section.Name = name;  
            section.Load();  
            return this.Add(section);  
        }  
  
        public Section Add(Section section)  
        {  
            this.m_items.Add(section);  
            return section;  
        }  
  
  
        public Section Add(char[] name)  
        {  
            return this.Add(new string(name));  
        }  
  
        public SectionCollection(Profile profile)  
        {  
            this.Profile = profile;  
        }  
  
        public int IndexOf(Section section, int startIndex)  
        {  
            return this.m_items.IndexOf(section, startIndex);  
        }  
  
        public int IndexOf(Section section)  
        {  
            return this.m_items.IndexOf(section);  
        }  
  
        public int LastIndexOf(Section section, int startIndex)  
        {  
            return this.m_items.LastIndexOf(section, startIndex);  
        }  
  
        public int LastIndexOf(Section section)  
        {  
            return this.m_items.LastIndexOf(section);  
        }  
  
        public void Remove(Section section)  
        {  
            this.m_items.Remove(section);  
        }  
  
        public void RemoveAt(int index)  
        {  
            this.m_items.Remove(index);  
        }  
  
        public void RemoveRange(int index, int count)  
        {  
            this.m_items.RemoveRange(index, count);  
        }  
  
        public void RemoveAll()  
        {  
            while (this.Count > 0)  
                this.RemoveAt(0);  
        }  
  
        public void Insert(int index, Section section)  
        {  
            this.m_items.Insert(index, section);  
        }  
  
        public void InsertRange(int index, ICollection c)  
        {  
            this.m_items.InsertRange(index, c);  
        }  
  
        public int Count  
        {  
            get  
            {  
                return this.m_items.Count;  
            }  
        }  
  
        public Section this[int index]  
        {  
            get  
            {  
                if (index < this.Count && index > -1)  
                    return this.m_items[index] as Section;  
                return null;  
            }  
        }  
    }  
  
    public class Section // 节  
    {  
        public string Name   
        {   
            get;  
            set;   
        }  
  
        public Section()  
        {  
            this.Keys = new KeyCollection();  
        }  
  
        public Profile Profile  
        {  
            get;   
            set;  
        }  
  
        public Section(Profile profile) : this()  
        {  
            this.Profile = profile;  
        }  
  
        public void Load()  
        {  
            if (File.Exists(this.Profile.FileName))  
            {  
                int i, len;  
                if ((len = this.Profile.Length) > 0)  
                {  
                    string buffer = new string('\0', len);  
                    if (Win32Native.GetPrivateProfileSection(this.Name, ref buffer, len, this.Profile.FileName) > 0)  
                    {  
                        string[] items = buffer.Split('\0');  
                        foreach (string str in items)  
                            if (str.Length > 0 && (i = str.IndexOf('=')) > -1)  
                                this.Keys.Add(str.Substring(0, i), str.Substring(i + 1));  
                    }  
                }                  
            }  
        }  
  
        public KeyCollection Keys  
        {  
            get;  
            set;  
        }  
  
    }  
  
    public class KeyCollection  
    {  
        private ArrayList m_items = new ArrayList();  
  
        public Key Add(string key, string value)  
        {  
            return this.Add(new Key() { Name = key, Value = value });  
        }  
  
        public Key Add(Key key)  
        {  
            this.m_items.Add(key);  
            return key;  
        }  
  
        public Key Add(char[] key, char[] value)  
        {  
            return this.Add(new string(key), new string(value));  
        }  
  
        public int IndexOf(Key key, int startIndex)  
        {  
            return this.m_items.IndexOf(key, startIndex);  
        }  
  
        public int IndexOf(Key key)  
        {  
            return this.m_items.IndexOf(key);  
        }  
  
        public int LastIndexOf(Key key, int startIndex)  
        {  
            return this.m_items.LastIndexOf(key, startIndex);  
        }  
  
        public int LastIndexOf(Key key)  
        {  
            return this.m_items.LastIndexOf(key);  
        }  
  
        public void Remove(Key key)  
        {  
            this.m_items.Remove(key);  
        }  
  
        public void RemoveAt(int index)  
        {  
            this.m_items.Remove(index);  
        }  
  
        public void RemoveRange(int index, int count)  
        {  
            this.m_items.RemoveRange(index, count);  
        }  
  
        public void RemoveAll()  
        {  
            while (this.Count > 0)  
  
                this.RemoveAt(0);  
        }  
  
        public void Insert(int index, Key section)  
        {  
            this.m_items.Insert(index, section);  
        }  
  
        public void InsertRange(int index, ICollection c)  
        {  
            this.m_items.InsertRange(index, c);  
        }  
  
        public int Count  
        {  
            get  
            {  
                return this.m_items.Count;  
            }  
        }  
  
        public Key this[int index]  
        {  
            get  
            {  
                if (index < this.Count && index > -1)  
                    return this.m_items[index] as Key;  
                return null;  
            }  
        }  
    }  
  
    public class Key  
    {  
        public string Name  
        {   
            get;   
            set;  
        }  
  
        public string Value   
        {  
            get;   
            set;   
        }  
    }  
}  
  

转自:http://blog.csdn.net/u012395622/article/details/46388709