Exemple #1
0
        /// <summary>
        /// Parses the file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="separation">The separation.</param>
        /// <param name="htmlEncodeValues">if set to <c>true</c> [HTML encode values].</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        public bool ParseFile(string filename, char separation, bool htmlEncodeValues, System.Text.Encoding encoding)
        {
            string buffer, lastGroup = string.Empty, groupName, keyName, keyValue;
            //string[] tempArray;

            // Try to open the ini file. If it doen't exist, the file is created.
            FileStream iniFile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read);

            // Initialize the streamreader object to read the content of the file then go
            // to the begin of the file.
            StreamReader fileRead = new StreamReader(iniFile, encoding);

            fileRead.BaseStream.Seek(0, SeekOrigin.Begin);

            // Now read all the file till the end.
            while (fileRead.Peek() > -1)
            {
                // Gets the line into the buffer string.
                buffer = fileRead.ReadLine().Trim();

                // Test if the string contain a subcategory delimiter.
                if (buffer.StartsWith("["))
                {
                    groupName = ParseGroup(buffer);
                    // Create a new item in the reference list.
                    if (!this.Groups.Contains(groupName))
                    {
                        this.Groups.Add(groupName);
                        lastGroup = groupName;
                    }
                }
                // Test if the string actually contain something intersting.
                else if (buffer.Length > 1 && buffer.IndexOf(separation) > -1 && !buffer.StartsWith(";") && !buffer.StartsWith("'"))
                {
                    // Split the string to get the Key and Value.
                    keyName  = buffer.Substring(0, buffer.IndexOf(separation));
                    keyValue = buffer.Substring(buffer.IndexOf(separation) + 1, buffer.Length - buffer.IndexOf(separation) - 1);

                    if (htmlEncodeValues)
                    {
                        keyValue = HtmlElementEncoder.EncodeValue(keyValue);
                    }
                    //tempArray = buffer.Split(separation);

                    //if (tempArray.Length > 1 && this.Groups.Contains(lastGroup))
                    if (keyName != string.Empty && this.Groups.Contains(lastGroup))
                    {
                        this.Groups[lastGroup].Keys.Add(keyName, keyValue);
                    }
                    //this.Groups[lastGroup].Keys.Add(tempArray[0], tempArray[1]);
                }
            }

            fileRead.Close();

            return(true);
        }