Пример #1
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);
     }
 }
Пример #2
0
        public override bool Equals(object obj)
        {
            if (!(obj is IniFileSectionItem))
            {
                return(false);
            }
            IniFileSectionItem item = (IniFileSectionItem)obj;

            return(this.Key == item.Key);
        }
Пример #3
0
        static IniFileSectionItem()
        {
            IniFileSectionItem item = new IniFileSectionItem {
                Key     = string.Empty,
                Value   = string.Empty,
                Comment = new List <string>()
            };

            None = item;
        }
Пример #4
0
 public IniFileSectionItem(string key, string value)
 {
     this = new IniFileSectionItem();
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     if (key.Length == 0)
     {
         throw new ArgumentOutOfRangeException("key");
     }
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     this.Key     = key;
     this.Value   = value;
     this.Comment = new List <string>();
 }
Пример #5
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;
         }
     }
 }
Пример #6
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);
         }
     }
 }
Пример #7
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);
        }
Пример #8
0
 private static string smethod_0(IniFileSectionItem iniFileSectionItem_0)
 {
     return(iniFileSectionItem_0.Value);
 }