Example #1
0
        /// <summary>
        /// Reads the entire initialization file in to memory.
        /// </summary>
        /// <returns> True if the read was successful and false otherwise. </returns>
        public bool Read()
        {
            try
            {
                using (StreamReader iniFileReader = new StreamReader(filePath))
                {
                    IniFileSection currentSection = null;

                    while (!iniFileReader.EndOfStream)
                    {
                        string currentLine = iniFileReader.ReadLine();
                        currentLine.TrimStart(); // remove leading white spaces

                        // if the current line has no size then move on to the next line
                        if (currentLine.Length <= 0)
                        {
                            continue;
                        }

                        // if comment line, and no section encountered yet then save as a file comment
                        if (currentSection == null && currentLine[0] == commentChar)
                        {
                            Comments.Add(currentLine.Substring(1).Trim());
                        }

                        // if there is a known section and the current line is a comment then
                        // add the comment to the section, but strip the comment character first
                        else if (currentLine[0] == commentChar)
                        {
                            currentSection.Comments.Add(currentLine.Substring(1).Trim());
                        }

                        // check if the current line is the start of a new section
                        else if (currentLine[0] == '[')
                        {
                            string sectionName = currentLine.Replace("[", "").Replace("]", "").Trim();  // get the section name by removing [ ]
                            currentSection = new IniFileSection(sectionName);                           // create the new section
                            sections.Add(currentSection);                                               // add the current section to the dictionary
                        }

                        // if this is not a comment or a section then it has to be an entry,
                        // but do a check to make sure there is a section to put it in
                        if (currentSection != null)
                        {
                            string[] lineSplit = currentLine.Split('=');

                            // make sure there are at least two parts,
                            // first part being the key and second the value
                            if (lineSplit.Length >= 2)
                            {
                                string   key        = lineSplit[0].Trim();
                                string[] valueSplit = lineSplit[1].Split(commentChar);

                                if (valueSplit.Length >= 1)
                                {
                                    string       value    = valueSplit[0]; // comment has to come after the value
                                    IniFileEntry newEntry = new IniFileEntry(key, value.Trim());

                                    // if there is a comment and it is not empty or null then save it
                                    if (valueSplit.Length > 1 && !string.IsNullOrEmpty(valueSplit[1].Trim()))
                                    {
                                        newEntry.Comment = valueSplit[1].Trim();
                                    }

                                    // add the new entry to the current section
                                    currentSection.Entries.Add(newEntry);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // if any errors happened return false
#if DEBUG
                System.Diagnostics.Debug.WriteLine("Exception in IniFile: " + ex.ToString());
#endif
                return(false);
            }

            return(true);
        }
Example #2
0
 /// <summary>
 /// Adds the IniFileSection to the Dictionary object containing entries.
 /// </summary>
 /// <param name="dictionary"> The Dictionary object that will add the entry. </param>
 /// <param name="entry"> The entry to add to the Dictionary object containing all entries. </param>
 public static void Add(this Dictionary <string, IniFileEntry> dictionary, IniFileEntry entry)
 {
     dictionary.Add(entry.Key, entry);
 }