Esempio n. 1
0
        private void LoadFile()
        {
            _FileContents = new ConcurrentDictionary <string, ConcurrentDictionary <string, string> >();
            if (string.IsNullOrEmpty(this.FileName))
            {
                return;
            }

            string Contents = CFile.GetFileContents(FileName);
            Regex  Section  = new Regex("[" + Regex.Escape(" ") + "\t]*" + Regex.Escape("[") + ".*" + Regex.Escape("]\r\n"));

            string[]        Sections       = Section.Split(Contents);
            MatchCollection SectionHeaders = Section.Matches(Contents);
            int             Counter        = 1;

            foreach (Match SectionHeader in SectionHeaders)
            {
                string[] Splitter      = { "\r\n" };
                string[] Splitter2     = { "=" };
                string[] Items         = Sections[Counter].Split(Splitter, StringSplitOptions.RemoveEmptyEntries);
                var      SectionValues = new ConcurrentDictionary <string, string>();
                foreach (string Item in Items)
                {
                    if (!String.IsNullOrWhiteSpace(Item))
                    {
                        SectionValues.TryAdd(
                            Item.Split(Splitter2, StringSplitOptions.None)[0].Trim(),
                            Item.Split(Splitter2, StringSplitOptions.None)[1].Trim());
                    }
                }
                _FileContents.TryAdd(SectionHeader.Value.Replace("[", "").Replace("]\r\n", "").Trim(), SectionValues);
                ++Counter;
            }
        }
Esempio n. 2
0
        private void WriteFile()
        {
            if (string.IsNullOrEmpty(this.FileName))
            {
                return;
            }
            StringBuilder Builder = new StringBuilder();

            foreach (string Header in _FileContents.Keys)
            {
                Builder.Append("[" + Header + "]\r\n");
                foreach (string Key in _FileContents[Header].Keys)
                {
                    Builder.Append(Key + "=" + _FileContents[Header][Key] + "\r\n");
                }
            }
            CFile.SaveFile(Builder.ToString(), FileName);
        }