Esempio n. 1
0
        /// <summary>
        /// 在指定配置节写入键值对。
        /// </summary>
        /// <param name="sectionName">指定配置节名称。</param>
        /// <param name="keyName">指定键名。</param>
        /// <param name="value">指定键值。</param>
        public void Write(string sectionName, string keyName, string value)
        {
            if (sectionName == null)
            {
                throw new ArgumentNullException("sectionName");
            }
            if (sectionName.Length == 0)
            {
                throw new ArgumentOutOfRangeException("sectionName");
            }

            if (keyName == null)
            {
                throw new ArgumentNullException("keyName");
            }
            if (keyName.Length == 0)
            {
                throw new ArgumentOutOfRangeException("keyName");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.Length == 0)
            {
                throw new ArgumentOutOfRangeException("value");
            }

            if (this.Sections.Contains(sectionName))
            {
                if (this[sectionName].Items.Contains(keyName))
                {
                    var item = this[sectionName][keyName];
                    item.Value = value;
                    this[sectionName].Items[keyName] = item;
                }
                else
                {
                    this[sectionName].Items.Add(new IniFileSectionItem {
                        Key     = keyName,
                        Value   = value,
                        Comment = new List <string>()
                    });
                }
            }
            else
            {
                var section = new IniFileSection(sectionName);
                section.Items.Add(new IniFileSectionItem {
                    Key     = keyName,
                    Value   = value,
                    Comment = new List <string>()
                });

                this.Sections.Add(section);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 刷新配置信息。
        /// </summary>
        public void Refresh()
        {
            var iniData = File.ReadAllLines(this.FileName, this.Encoding);

            if (iniData.Length == 0)
            {
                return;
            }

            var    nextItemComment = new List <string>();
            var    section         = IniFileSection.None;
            string dataBuffer;

            foreach (var data in iniData)
            {
                dataBuffer = data.Trim();

                // 空行
                if (String.IsNullOrEmpty(dataBuffer))
                {
                    // 作为文档注释
                    if (nextItemComment.Count > 0 && this.Sections.Count == 0)
                    {
                        this.Comment.AddRange(nextItemComment);

                        nextItemComment.Clear();
                    }

                    continue;
                }

                // 非法格式
                if (dataBuffer.StartsWith("="))
                {
                    nextItemComment.Clear();

                    continue;
                }

                // 注释格式,作为下一行的注释
                if (dataBuffer.StartsWith(";") || dataBuffer.StartsWith("#"))
                {
                    nextItemComment.Add(dataBuffer.Remove(0, 1).Trim());

                    if (nextItemComment.Count > 0)
                    {
                        continue;
                    }
                }

                // 配置节名称
                if (dataBuffer.StartsWith("[") && dataBuffer.EndsWith("]"))
                {
                    if (section != IniFileSection.None)
                    {
                        this.Sections.Add(section);
                    }

                    section = new IniFileSection(dataBuffer.Trim('[', ']'));

                    if (nextItemComment.Count > 0)
                    {
                        section.Comment.AddRange(nextItemComment);

                        nextItemComment.Clear();
                    }

                    continue;
                }

                // 无键值的项
                IniFileSectionItem item;
                var index = dataBuffer.IndexOf('=');

                if (index == -1)
                {
                    if (section != IniFileSection.None)
                    {
                        item = new IniFileSectionItem {
                            Key     = dataBuffer,
                            Value   = String.Empty,
                            Comment = new List <string>()
                        };

                        if (nextItemComment.Count > 0)
                        {
                            item.Comment.AddRange(nextItemComment);

                            nextItemComment.Clear();
                        }

                        section.Items.Add(item);
                    }

                    continue;
                }

                // 键值对
                if (section == IniFileSection.None)
                {
                    continue;
                }

                var key   = dataBuffer.Substring(0, index).Trim();
                var value = dataBuffer.Substring(index + 1).Trim();

                item = new IniFileSectionItem {
                    Key     = key,
                    Value   = value,
                    Comment = new List <string>()
                };

                if (nextItemComment.Count > 0)
                {
                    item.Comment.AddRange(nextItemComment);

                    nextItemComment.Clear();
                }

                section.Items.Add(item);
            }

            // 最后一项
            if (section == IniFileSection.None)
            {
                return;
            }

            if (nextItemComment.Count > 0)
            {
                section.Comment.AddRange(nextItemComment);

                nextItemComment.Clear();
            }

            this.Sections.Add(section);
        }