예제 #1
0
 public void DeleteKey(string sectionName, string keyName)
 {
     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 (this.Sections.Contains(sectionName))
     {
         IniFileSection section = this[sectionName];
         if (section.Items.Contains(keyName))
         {
             section = this[sectionName];
             section.Items.Remove(keyName);
         }
     }
 }
예제 #2
0
        public string Read(string sectionName, string keyName, string defaultValue)
        {
            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 (!this.Sections.Contains(sectionName))
            {
                return(defaultValue);
            }
            IniFileSection section = this[sectionName];

            return(!section.Items.Contains(keyName) ? defaultValue : (section = this[sectionName])[keyName].Value);
        }
예제 #3
0
 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))
     {
         IniFileSection section = this[sectionName];
         if (section.Items.Contains(keyName))
         {
             section = this[sectionName];
             IniFileSectionItem item = section[keyName];
             item.Value             = value;
             section                = this[sectionName];
             section.Items[keyName] = item;
         }
         else
         {
             section = this[sectionName];
             IniFileSectionItem item2 = new IniFileSectionItem {
                 Key     = keyName,
                 Value   = value,
                 Comment = new List <string>()
             };
             section.Items.Add(item2);
         }
     }
     else
     {
         IniFileSection     section2 = new IniFileSection(sectionName);
         IniFileSectionItem item3    = new IniFileSectionItem {
             Key     = keyName,
             Value   = value,
             Comment = new List <string>()
         };
         section2.Items.Add(item3);
         this.Sections.Add(section2);
     }
 }
예제 #4
0
        static IniFileSection()
        {
            IniFileSection section = new IniFileSection {
                Name = string.Empty
            };

            None = section;
        }
예제 #5
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);
            }
        }
예제 #6
0
        public override bool Equals(object obj)
        {
            if (!(obj is IniFileSection))
            {
                return(false);
            }
            IniFileSection section = (IniFileSection)obj;

            return(this.Name == section.Name);
        }
예제 #7
0
 public IniFileSection(string name)
 {
     this = new IniFileSection();
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (name.Length == 0)
     {
         throw new ArgumentOutOfRangeException("name");
     }
     this.Name    = name;
     this.Items   = new IniFileSectionItemCollection();
     this.Comment = new List <string>();
 }
예제 #8
0
 public void UpdateWrite(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))
     {
         IniFileSection section = this[sectionName];
         if (section.Items.Contains(keyName))
         {
             section = this[sectionName];
             IniFileSectionItem item = section[keyName];
             item.Value             = value;
             section                = this[sectionName];
             section.Items[keyName] = item;
         }
     }
 }
예제 #9
0
 public void Refresh()
 {
     string[] strArray = File.ReadAllLines(this.FileName, this.Encoding);
     if (strArray.Length != 0)
     {
         List <string>  collection = new List <string>();
         IniFileSection none       = IniFileSection.None;
         foreach (string str in strArray)
         {
             string str2 = str.Trim();
             if (string.IsNullOrEmpty(str2))
             {
                 if ((collection.Count > 0) && (this.Sections.Count == 0))
                 {
                     this.Comment.AddRange(collection);
                     collection.Clear();
                 }
             }
             else if (str2.StartsWith("="))
             {
                 collection.Clear();
             }
             else
             {
                 if (str2.StartsWith(";") || str2.StartsWith("#"))
                 {
                     collection.Add(str2.Remove(0, 1).Trim());
                     if (collection.Count > 0)
                     {
                         goto Label_028C;
                     }
                 }
                 if (str2.StartsWith("[") && str2.EndsWith("]"))
                 {
                     if (none != IniFileSection.None)
                     {
                         this.Sections.Add(none);
                     }
                     none = new IniFileSection(str2.Trim(new char[] { '[', ']' }));
                     if (collection.Count > 0)
                     {
                         none.Comment.AddRange(collection);
                         collection.Clear();
                     }
                 }
                 else
                 {
                     IniFileSectionItem item2;
                     int index = str2.IndexOf('=');
                     if (index == -1)
                     {
                         if (none != IniFileSection.None)
                         {
                             IniFileSectionItem item = new IniFileSectionItem {
                                 Key     = str2,
                                 Value   = string.Empty,
                                 Comment = new List <string>()
                             };
                             item2 = item;
                             if (collection.Count > 0)
                             {
                                 item2.Comment.AddRange(collection);
                                 collection.Clear();
                             }
                             none.Items.Add(item2);
                         }
                     }
                     else if (none != IniFileSection.None)
                     {
                         string             str3  = str2.Substring(0, index).Trim();
                         string             str4  = str2.Substring(index + 1).Trim();
                         IniFileSectionItem item3 = new IniFileSectionItem {
                             Key     = str3,
                             Value   = str4,
                             Comment = new List <string>()
                         };
                         item2 = item3;
                         if (collection.Count > 0)
                         {
                             item2.Comment.AddRange(collection);
                             collection.Clear();
                         }
                         none.Items.Add(item2);
                     }
                 }
                 Label_028C :;
             }
         }
         if (none != IniFileSection.None)
         {
             if (collection.Count > 0)
             {
                 none.Comment.AddRange(collection);
                 collection.Clear();
             }
             this.Sections.Add(none);
         }
     }
 }
예제 #10
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);
        }