コード例 #1
0
        /// <summary>
        /// Adds the given value to the given key in the given section
        /// </summary>
        /// <param name="sectionName">The section to add the value to</param>
        /// <param name="keyName">The key to add the value to</param>
        /// <param name="defaultValue">The value to be added</param>
        public void WriteString(string sectionName, string keyName, string defaultValue)
        {
            // Check if the dictionary contains the given section
            if (!_Sections.ContainsKey(sectionName))
            {
                // It doesn't, so we need to create it
                _Sections[sectionName] = new IniFileSection();
                _SectionNames.Add(sectionName);
            }

            // Update the modified flag
            _Modified |= _Sections[sectionName].WriteString(keyName, defaultValue);

            // If we want to write all updates immediately, we should do so here
            if (_Modified && AutoSave)
            {
                Save();
            }
        }
コード例 #2
0
ファイル: IniFile.cs プロジェクト: Robin--/RMLib
        /// <summary>
        /// Custom constructor to load the given INI into memory, and indicate whether the changes should be buffered or immediately written to disk
        /// </summary>
        /// <param name="fileName">The INI to load</param>
        /// <param name="password">The password used to encrypt/decrypt the contents of the INI file</param>
        public IniFile(string fileName, RMSecureString password)
        {
            AutoSave = false;
            Password = new RMSecureString();

            _FileName = fileName;
            Password = password;

            List<string> CurrentComment = new List<string>();
            string CurrentSection = "";
            if (File.Exists(fileName))
            {
                // Read in the INI file
                string FileText = FileUtils.FileReadAllText(fileName, RMEncoding.Ansi);

                // Decrypt the INI file (if necessary)
                if (password.Length > 0)
                {
                    try
                    {
                        FileText = AES.Decrypt(FileText, password.GetPlainText(), INI_FILE_SALT);
                        Password = password;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }

                // Split the INI file into separate lines
                string[] Lines = FileText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');

                // Loop through each line
                foreach (string Line in Lines)
                {
                    // Make sure the line isn't empty
                    if ((!string.IsNullOrEmpty(Line.Trim())))
                    {
                        // Check if this is a comment
                        if (Line.TrimStart().StartsWith(";"))
                        {
                            if (string.IsNullOrEmpty(CurrentSection))
                            {
                                _HeaderComment.Add(Line.TrimStart().Substring(1));
                            }
                            else
                            {
                                CurrentComment.Add(Line.TrimStart().Substring(1));
                            }
                        }
                        else
                        {
                            // Check if this is a new section
                            if ((Line.TrimStart().StartsWith("[")) && (Line.TrimEnd().EndsWith("]")))
                            {
                                // It is, so add the new section
                                CurrentSection = Line.Trim().Substring(1, Line.Trim().Length - 2);
                                _Sections[CurrentSection] = new IniFileSection(CurrentComment);
                                _SectionNames.Add(CurrentSection);
                                CurrentComment = new List<string>();
                            }
                            else
                            {
                                // It isn't so add the key/value to the current section
                                if (!string.IsNullOrEmpty(CurrentSection))
                                {
                                    // Make sure this line is in the KEY=VALUE form
                                    int EqualPos = Line.IndexOf('=');
                                    if (EqualPos >= 1)
                                    {
                                        // Get the key
                                        string Key = Line.Substring(0, EqualPos);

                                        // Get the value
                                        string Value = Line.Substring(EqualPos + 1);

                                        // Add the key/value pair to the dictionary
                                        _Sections[CurrentSection].WriteString(CurrentComment, Key, Value);
                                        CurrentComment = new List<string>();
                                    }
                                }
                            }
                        }
                    }
                }

                // Ensure the supplied password is valid
                if (Password.Length > 0)
                {
                    if ((_HeaderComment.Count == 0) || (_HeaderComment[0] != INI_FILE_ENCRYPTED_HEADER))
                    {
                        // Password did not properly decrypt the ini file
                        _Sections.Clear();
                        _SectionNames.Clear();
                        return;
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Custom constructor to load the given INI into memory, and indicate whether the changes should be buffered or immediately written to disk
        /// </summary>
        /// <param name="fileName">The INI to load</param>
        /// <param name="password">The password used to encrypt/decrypt the contents of the INI file</param>
        public IniFile(string fileName, RMSecureString password)
        {
            AutoSave = false;
            Password = new RMSecureString();

            _FileName = fileName;
            Password  = password;

            List <string> CurrentComment = new List <string>();
            string        CurrentSection = "";

            if (File.Exists(fileName))
            {
                // Read in the INI file
                string FileText = FileUtils.FileReadAllText(fileName, RMEncoding.Ansi);

                // Decrypt the INI file (if necessary)
                if (password.Length > 0)
                {
                    try
                    {
                        FileText = AES.Decrypt(FileText, password.GetPlainText(), INI_FILE_SALT);
                        Password = password;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }

                // Split the INI file into separate lines
                string[] Lines = FileText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');

                // Loop through each line
                foreach (string Line in Lines)
                {
                    // Make sure the line isn't empty
                    if ((!string.IsNullOrEmpty(Line.Trim())))
                    {
                        // Check if this is a comment
                        if (Line.TrimStart().StartsWith(";"))
                        {
                            if (string.IsNullOrEmpty(CurrentSection))
                            {
                                _HeaderComment.Add(Line.TrimStart().Substring(1));
                            }
                            else
                            {
                                CurrentComment.Add(Line.TrimStart().Substring(1));
                            }
                        }
                        else
                        {
                            // Check if this is a new section
                            if ((Line.TrimStart().StartsWith("[")) && (Line.TrimEnd().EndsWith("]")))
                            {
                                // It is, so add the new section
                                CurrentSection            = Line.Trim().Substring(1, Line.Trim().Length - 2);
                                _Sections[CurrentSection] = new IniFileSection(CurrentComment);
                                _SectionNames.Add(CurrentSection);
                                CurrentComment = new List <string>();
                            }
                            else
                            {
                                // It isn't so add the key/value to the current section
                                if (!string.IsNullOrEmpty(CurrentSection))
                                {
                                    // Make sure this line is in the KEY=VALUE form
                                    int EqualPos = Line.IndexOf('=');
                                    if (EqualPos >= 1)
                                    {
                                        // Get the key
                                        string Key = Line.Substring(0, EqualPos);

                                        // Get the value
                                        string Value = Line.Substring(EqualPos + 1);

                                        // Add the key/value pair to the dictionary
                                        _Sections[CurrentSection].WriteString(CurrentComment, Key, Value);
                                        CurrentComment = new List <string>();
                                    }
                                }
                            }
                        }
                    }
                }

                // Ensure the supplied password is valid
                if (Password.Length > 0)
                {
                    if ((_HeaderComment.Count == 0) || (_HeaderComment[0] != INI_FILE_ENCRYPTED_HEADER))
                    {
                        // Password did not properly decrypt the ini file
                        _Sections.Clear();
                        _SectionNames.Clear();
                        return;
                    }
                }
            }
        }
コード例 #4
0
ファイル: IniFile.cs プロジェクト: Robin--/RMLib
        /// <summary>
        /// Adds the given value to the given key in the given section
        /// </summary>
        /// <param name="sectionName">The section to add the value to</param>
        /// <param name="keyName">The key to add the value to</param>
        /// <param name="defaultValue">The value to be added</param>
        public void WriteString(string sectionName, string keyName, string defaultValue)
        {
            // Check if the dictionary contains the given section
            if (!_Sections.ContainsKey(sectionName))
            {
                // It doesn't, so we need to create it
                _Sections[sectionName] = new IniFileSection();
                _SectionNames.Add(sectionName);
            }

            // Update the modified flag
            _Modified |= _Sections[sectionName].WriteString(keyName, defaultValue);

            // If we want to write all updates immediately, we should do so here
            if (_Modified && AutoSave) Save();
        }