Beispiel #1
0
        /// <summary>
        /// Deletes property from section in configuration file.
        /// </summary>
        /// <param name="section">The section of the property.</param>
        /// <param name="key">The key of the property.</param>
        public void Delete(string section, string key)
        {
            IniSection properties = this.GetSection(section);

            if (properties == null)
            {
                return;
            }

            // If the key is the last property, might as well delete section.
            if (properties.Contains(key) && properties.Count == 1)
            {
                p_Sections.Remove(section);
            }
            else
            {
                properties.Remove(key);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Loads the properties from the configuration file.
        /// </summary>
        public void Load()
        {
            if (!File.Exists(this.Filename))
            {
                return;
            }

            this.p_Sections.Clear();

            using (var reader = new StreamReader(this.Filename))
            {
                string     key = string.Empty;
                string     line;
                int        lineCount = 0;
                IniSection section   = this.GetSection(SectionHeader);

                while ((line = reader.ReadLine()) != null)
                {
                    lineCount++;
                    line = line.Trim();

                    // Empty line.
                    if (string.IsNullOrEmpty(line))
                    {
                        key = IniType.EmptyLine.ToString() + lineCount;

                        // Use key above if available, otherwise generate random
                        if (section.Contains(key))
                        {
                            section.Add(new IniProperty(section.Name, IniType.EmptyLine, line));
                        }
                        else
                        {
                            section.Add(new IniProperty(section.Name, IniType.EmptyLine, key, line));
                        }
                    }
                    else
                    {
                        char firstChar = char.Parse(line.Substring(0, 1));

                        switch (firstChar)
                        {
                        case ';':
                        case '#':     // Comment line
                            key = IniType.Comment.ToString() + lineCount;

                            // Use key above if available, otherwise generate random
                            if (section.Contains(key))
                            {
                                section.Add(new IniProperty(section.Name, IniType.Comment, line));
                            }
                            else
                            {
                                section.Add(new IniProperty(section.Name, IniType.Comment, key, line));
                            }

                            break;

                        case '[':     // Section line
                            if (!line.EndsWith("]"))
                            {
                                // Line doesn't end with a closing bracket.
                                goto default;
                            }
                            else
                            {
                                int start  = line.IndexOf('[') + 1;
                                int length = line.IndexOf(']') - line.IndexOf('[') - 1;

                                string name = line.Substring(start, length);

                                if (p_Sections.Contains(name))
                                {
                                    section = (IniSection)p_Sections[name];
                                }
                                else
                                {
                                    section = new IniSection(name);

                                    p_Sections.Add(name, section);
                                }
                            }
                            break;

                        default:
                            // Valid property line
                            if (line.Contains("=") && section.Name != SectionHeader)
                            {
                                string[] split = line.Split('=');
                                string   value = split[1];

                                key = split[0];

                                if (section.Contains(split[0]))
                                {
                                    throw new Exception(string.Format("Section '{0}' already contains property '{1}'. Value: {2}", section.Name, key, value));
                                }

                                section.Add(new IniProperty(section.Name, key, value));
                            }
                            else     // Invalid line
                            {
                                key = IniType.Invalid.ToString() + lineCount;

                                // Use key above if available, otherwise generate random
                                if (section.Contains(key))
                                {
                                    section.Add(new IniProperty(section.Name, IniType.Invalid, line));
                                }
                                else
                                {
                                    section.Add(new IniProperty(section.Name, IniType.Invalid, key, line));
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }