INI配置文件C#類

ny8p 9年前發布 | 1K 次閱讀 C#
在這個漫天都是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
 本文由用戶 ny8p 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!