| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 | using System.Collections.Generic;using System.IO;namespace Wayne.Lib.IO{                public class IniFile    {        private readonly IFileSupport fileSupport;                                        public IniFile(IServiceLocator serviceLocator)        {            fileSupport = serviceLocator.GetService<IFileSupport>();            Sections = new Dictionary<string, IniFileSection>();        }                                public Dictionary<string, IniFileSection> Sections { get; private set; }                                                public IniFileSection GetSection(string sectionName)        {            IniFileSection section;            if (!Sections.TryGetValue(sectionName, out section))            {                section = new IniFileSection();                Sections.Add(sectionName, section);            }            return section;        }                                        public void SaveToFile(string fileName)        {            using (Stream stream = fileSupport.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))            {                using (StreamWriter streamWriter = new StreamWriter(stream))                {                    foreach (KeyValuePair<string, IniFileSection> sectionValuePair in Sections)                    {                        streamWriter.WriteLine(string.Concat("[", sectionValuePair.Key, "]"));                        foreach (KeyValuePair<string, string> valuePair in sectionValuePair.Value.Values)                        {                            if (valuePair.Value != null)                                streamWriter.WriteLine(string.Concat(valuePair.Key.Trim(), "=", valuePair.Value.Trim()));                            else                                streamWriter.WriteLine(valuePair.Key.Trim());                        }                        streamWriter.WriteLine();                    }                }            }        }                                        public void LoadFromFile(string fileName)        {            Sections.Clear();            using (Stream stream = fileSupport.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))            {                using (StreamReader streamReader = new StreamReader(stream))                {                    IniFileSection currentSection = null;                    while (!streamReader.EndOfStream)                    {                        string line = streamReader.ReadLine().Trim();                        if (!string.IsNullOrEmpty(line))                        {                            if (line.StartsWith("["))                            {                                if (line.EndsWith("]"))                                {                                    string sectionName = line.Substring(1, line.Length - 2);                                    currentSection = new IniFileSection();                                    Sections[sectionName] = currentSection;                                }                                else                                {                                                                    }                            }                            else if (currentSection != null)                            {                                int equalSignIndex = line.IndexOf('=');                                if (equalSignIndex > -1)                                {                                    string key = line.Substring(0, equalSignIndex);                                    string value = line.Substring(equalSignIndex + 1);                                    currentSection.Values[key] = value;                                }                                else                                    currentSection.Values[line] = null;                            }                            else                            {                                                            }                        }                    }                }            }        }    }                public class IniFileSection    {                                public IniFileSection()        {            Values = new Dictionary<string, string>();        }                                public Dictionary<string, string> Values { get; private set; }    }}
 |