Ejemplo n.º 1
0
        /// <summary>
        /// Advanced delete method that allows you to specify the section to delete
        /// </summary>
        /// <param name="sectionName">The section to delete within the INI</param>
        protected void Delete(string sectionName)
        {
            // Store the section name
            SectionName = sectionName;

            // Load the application ini
            using (IniFile Ini = new IniFile(FileName, IniPassword))
            {
                // Check if the desired section exists
                if (Ini.SectionExists(sectionName))
                {
                    // Yep, so delete it
                    Ini.EraseSection(sectionName);
                    Ini.Save();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Advanced load method that allows you to specify the section to read from
        /// </summary>
        /// <param name="sectionName">The section to read within the INI</param>
        /// <returns>true if the INI section existed; false otherwise</returns>
        protected bool Load(string sectionName)
        {
            // Store the section name
            SectionName = sectionName;

            // Load the application ini
            using (IniFile Ini = new IniFile(FileName, IniPassword))
            {
                // Check if the desired section exists
                if (!Ini.SectionExists(sectionName))
                {
                    // Nope, so abort
                    return(false);
                }

                // Loop through each field in the inherited class and read the value from the Ini
                PropertyInfo[] Properties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                foreach (PropertyInfo Property in Properties)
                {
                    // Ensure we only look at read+write properties (read only helper properties should not be loaded from/saved to an ini)
                    if ((Property.CanRead) && (Property.CanWrite))
                    {
                        switch (Property.PropertyType.Name)
                        {
                        case "Boolean": Property.SetValue(this, Ini.ReadBoolean(sectionName, Property.Name, (Boolean)Property.GetValue(this, null)), null); break;

                        case "Boolean[]": Property.SetValue(this, Ini.ReadBoolean(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Byte": Property.SetValue(this, Ini.ReadByte(sectionName, Property.Name, (Byte)Property.GetValue(this, null)), null); break;

                        case "Byte[]": Property.SetValue(this, Ini.ReadByte(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Char": Property.SetValue(this, Ini.ReadChar(sectionName, Property.Name, (Char)Property.GetValue(this, null)), null); break;

                        case "Char[]": Property.SetValue(this, Ini.ReadChar(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "DateTime": Property.SetValue(this, Ini.ReadDateTime(sectionName, Property.Name, (DateTime)Property.GetValue(this, null)), null); break;

                        case "DateTime[]": Property.SetValue(this, Ini.ReadDateTime(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Decimal": Property.SetValue(this, Ini.ReadDecimal(sectionName, Property.Name, (Decimal)Property.GetValue(this, null)), null); break;

                        case "Decimal[]": Property.SetValue(this, Ini.ReadDecimal(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Double": Property.SetValue(this, Ini.ReadDouble(sectionName, Property.Name, (Double)Property.GetValue(this, null)), null); break;

                        case "Double[]": Property.SetValue(this, Ini.ReadDouble(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Int16": Property.SetValue(this, Ini.ReadInt16(sectionName, Property.Name, (Int16)Property.GetValue(this, null)), null); break;

                        case "Int16[]": Property.SetValue(this, Ini.ReadInt16(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Int32": Property.SetValue(this, Ini.ReadInt32(sectionName, Property.Name, (Int32)Property.GetValue(this, null)), null); break;

                        case "Int32[]": Property.SetValue(this, Ini.ReadInt32(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Int64": Property.SetValue(this, Ini.ReadInt64(sectionName, Property.Name, (Int64)Property.GetValue(this, null)), null); break;

                        case "Int64[]": Property.SetValue(this, Ini.ReadInt64(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "SByte": Property.SetValue(this, Ini.ReadSByte(sectionName, Property.Name, (SByte)Property.GetValue(this, null)), null); break;

                        case "SByte[]": Property.SetValue(this, Ini.ReadSByte(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "Single": Property.SetValue(this, Ini.ReadSingle(sectionName, Property.Name, (Single)Property.GetValue(this, null)), null); break;

                        case "Single[]": Property.SetValue(this, Ini.ReadSingle(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "String": Property.SetValue(this, Ini.ReadString(sectionName, Property.Name, Property.GetValue(this, null).ToString()), null); break;

                        case "String[]": Property.SetValue(this, Ini.ReadString(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "UInt16": Property.SetValue(this, Ini.ReadUInt16(sectionName, Property.Name, (UInt16)Property.GetValue(this, null)), null); break;

                        case "UInt16[]": Property.SetValue(this, Ini.ReadUInt16(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "UInt32": Property.SetValue(this, Ini.ReadUInt32(sectionName, Property.Name, (UInt32)Property.GetValue(this, null)), null); break;

                        case "UInt32[]": Property.SetValue(this, Ini.ReadUInt32(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "UInt64": Property.SetValue(this, Ini.ReadUInt64(sectionName, Property.Name, (UInt64)Property.GetValue(this, null)), null); break;

                        case "UInt64[]": Property.SetValue(this, Ini.ReadUInt64(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;

                        case "RMSecureString":
                            string Enc = Ini.ReadString(sectionName, Property.Name, "");
                            if (Enc.Length > 0)
                            {
                                RMSecureString RMSS = new RMSecureString();
                                try
                                {
                                    if (RMSecureStringPassword.Length == 0)
                                    {
                                        // No password means protected string
                                        RMSS.LoadFromProtectedString(Enc, RMSecureStringPassword);
                                    }
                                    else
                                    {
                                        // Password means encrypted string
                                        RMSS.LoadFromEncryptedString(Enc, RMSecureStringPassword);
                                    }
                                }
                                catch (Exception)
                                {
                                    // Loading failed -- could be that the protection happened under a different user account, or the password is incorrect
                                    // TODO Should really save the exception and throw it later I think
                                    RMSS = new RMSecureString();
                                }
                                Property.SetValue(this, RMSS, null);
                            }
                            break;

                        case "StringDictionary":
                            StringDictionary SD = new StringDictionary();

                            string[] Keys = Ini.ReadSection(sectionName);
                            foreach (string Key in Keys)
                            {
                                if (Key.IndexOf(Property.Name + "_", StringComparison.OrdinalIgnoreCase) == 0)
                                {
                                    string KeyWithoutPrefix = Key.Substring(Property.Name.Length + 1);
                                    SD.Add(KeyWithoutPrefix, Ini.ReadString(sectionName, Key, ""));
                                }
                            }

                            Property.SetValue(this, SD, null);
                            //string Section = Property.Name.ToUpper();
                            //string[] Keys = Ini.ReadSection(Section);

                            //StringDictionary SD = new StringDictionary();
                            //foreach (string Key in Keys)
                            //{
                            //    SD.Add(Key, Ini.ReadString(Section, Key, ""));
                            //}
                            //Property.SetValue(this, SD, null);
                            break;

                        default:
                            // Check for enum, which we can try to parse
                            if (Property.PropertyType.BaseType.Name == "Array")
                            {
                                List <int> EnumValues   = new List <int>();
                                string[]   StringValues = Ini.ReadString(sectionName, Property.Name, (IList)Property.GetValue(this, null));
                                foreach (string StringValue in StringValues)
                                {
                                    EnumValues.Add((int)Enum.Parse(Property.PropertyType.GetElementType(), StringValue));
                                }
                                Property.SetValue(this, EnumValues.ToArray(), null);
                            }
                            else if (Property.PropertyType.BaseType.Name == "Enum")
                            {
                                Property.SetValue(this, Enum.Parse(Property.PropertyType, Ini.ReadString(sectionName, Property.Name, Property.GetValue(this, null).ToString())), null);
                            }
                            break;
                        }
                    }
                }
            }

            Loaded = true;
            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Advanced load method that allows you to specify the section to read from
        /// </summary>
        /// <param name="sectionName">The section to read within the INI</param>
        /// <returns>true if the INI section existed; false otherwise</returns>
        protected bool Load(string sectionName)
        {
            // Store the section name
            SectionName = sectionName;

            // Load the application ini
            using (IniFile Ini = new IniFile(FileName, IniPassword))
            {

                // Check if the desired section exists
                if (!Ini.SectionExists(sectionName))
                {
                    // Nope, so abort
                    return false;
                }

                // Loop through each field in the inherited class and read the value from the Ini
                PropertyInfo[] Properties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                foreach (PropertyInfo Property in Properties)
                {
                    // Ensure we only look at read+write properties (read only helper properties should not be loaded from/saved to an ini)
                    if ((Property.CanRead) && (Property.CanWrite))
                    {
                        switch (Property.PropertyType.Name)
                        {
                            case "Boolean": Property.SetValue(this, Ini.ReadBoolean(sectionName, Property.Name, (Boolean)Property.GetValue(this, null)), null); break;
                            case "Boolean[]": Property.SetValue(this, Ini.ReadBoolean(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Byte": Property.SetValue(this, Ini.ReadByte(sectionName, Property.Name, (Byte)Property.GetValue(this, null)), null); break;
                            case "Byte[]": Property.SetValue(this, Ini.ReadByte(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Char": Property.SetValue(this, Ini.ReadChar(sectionName, Property.Name, (Char)Property.GetValue(this, null)), null); break;
                            case "Char[]": Property.SetValue(this, Ini.ReadChar(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "DateTime": Property.SetValue(this, Ini.ReadDateTime(sectionName, Property.Name, (DateTime)Property.GetValue(this, null)), null); break;
                            case "DateTime[]": Property.SetValue(this, Ini.ReadDateTime(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Decimal": Property.SetValue(this, Ini.ReadDecimal(sectionName, Property.Name, (Decimal)Property.GetValue(this, null)), null); break;
                            case "Decimal[]": Property.SetValue(this, Ini.ReadDecimal(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Double": Property.SetValue(this, Ini.ReadDouble(sectionName, Property.Name, (Double)Property.GetValue(this, null)), null); break;
                            case "Double[]": Property.SetValue(this, Ini.ReadDouble(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Int16": Property.SetValue(this, Ini.ReadInt16(sectionName, Property.Name, (Int16)Property.GetValue(this, null)), null); break;
                            case "Int16[]": Property.SetValue(this, Ini.ReadInt16(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Int32": Property.SetValue(this, Ini.ReadInt32(sectionName, Property.Name, (Int32)Property.GetValue(this, null)), null); break;
                            case "Int32[]": Property.SetValue(this, Ini.ReadInt32(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Int64": Property.SetValue(this, Ini.ReadInt64(sectionName, Property.Name, (Int64)Property.GetValue(this, null)), null); break;
                            case "Int64[]": Property.SetValue(this, Ini.ReadInt64(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "SByte": Property.SetValue(this, Ini.ReadSByte(sectionName, Property.Name, (SByte)Property.GetValue(this, null)), null); break;
                            case "SByte[]": Property.SetValue(this, Ini.ReadSByte(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "Single": Property.SetValue(this, Ini.ReadSingle(sectionName, Property.Name, (Single)Property.GetValue(this, null)), null); break;
                            case "Single[]": Property.SetValue(this, Ini.ReadSingle(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "String": Property.SetValue(this, Ini.ReadString(sectionName, Property.Name, Property.GetValue(this, null).ToString()), null); break;
                            case "String[]": Property.SetValue(this, Ini.ReadString(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "UInt16": Property.SetValue(this, Ini.ReadUInt16(sectionName, Property.Name, (UInt16)Property.GetValue(this, null)), null); break;
                            case "UInt16[]": Property.SetValue(this, Ini.ReadUInt16(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "UInt32": Property.SetValue(this, Ini.ReadUInt32(sectionName, Property.Name, (UInt32)Property.GetValue(this, null)), null); break;
                            case "UInt32[]": Property.SetValue(this, Ini.ReadUInt32(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "UInt64": Property.SetValue(this, Ini.ReadUInt64(sectionName, Property.Name, (UInt64)Property.GetValue(this, null)), null); break;
                            case "UInt64[]": Property.SetValue(this, Ini.ReadUInt64(sectionName, Property.Name, (IList)Property.GetValue(this, null)), null); break;
                            case "RMSecureString":
                                string Enc = Ini.ReadString(sectionName, Property.Name, "");
                                if (Enc.Length > 0)
                                {
                                    RMSecureString RMSS = new RMSecureString();
                                    try
                                    {
                                        if (RMSecureStringPassword.Length == 0)
                                        {
                                            // No password means protected string
                                            RMSS.LoadFromProtectedString(Enc, RMSecureStringPassword);
                                        }
                                        else
                                        {
                                            // Password means encrypted string
                                            RMSS.LoadFromEncryptedString(Enc, RMSecureStringPassword);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        // Loading failed -- could be that the protection happened under a different user account, or the password is incorrect
                                        // TODO Should really save the exception and throw it later I think
                                        RMSS = new RMSecureString();
                                    }
                                    Property.SetValue(this, RMSS, null);
                                }
                                break;
                            case "StringDictionary":
                                StringDictionary SD = new StringDictionary();

                                string[] Keys = Ini.ReadSection(sectionName);
                                foreach (string Key in Keys)
                                {
                                    if (Key.IndexOf(Property.Name + "_", StringComparison.OrdinalIgnoreCase) == 0)
                                    {
                                        string KeyWithoutPrefix = Key.Substring(Property.Name.Length + 1);
                                        SD.Add(KeyWithoutPrefix, Ini.ReadString(sectionName, Key, ""));
                                    }
                                }

                                Property.SetValue(this, SD, null);
                                //string Section = Property.Name.ToUpper();
                                //string[] Keys = Ini.ReadSection(Section);

                                //StringDictionary SD = new StringDictionary();
                                //foreach (string Key in Keys)
                                //{
                                //    SD.Add(Key, Ini.ReadString(Section, Key, ""));
                                //}
                                //Property.SetValue(this, SD, null);
                                break;
                            default:
                                // Check for enum, which we can try to parse
                                if (Property.PropertyType.BaseType.Name == "Array")
                                {
                                    List<int> EnumValues = new List<int>();
                                    string[] StringValues = Ini.ReadString(sectionName, Property.Name, (IList)Property.GetValue(this, null));
                                    foreach (string StringValue in StringValues)
                                    {
                                        EnumValues.Add((int)Enum.Parse(Property.PropertyType.GetElementType(), StringValue));
                                    }
                                    Property.SetValue(this, EnumValues.ToArray(), null);
                                }
                                else if (Property.PropertyType.BaseType.Name == "Enum")
                                {
                                    Property.SetValue(this, Enum.Parse(Property.PropertyType, Ini.ReadString(sectionName, Property.Name, Property.GetValue(this, null).ToString())), null);
                                }
                                break;
                        }
                    }
                }
            }

            Loaded = true;
            return true;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Advanced delete method that allows you to specify the section to delete
        /// </summary>
        /// <param name="sectionName">The section to delete within the INI</param>
        protected void Delete(string sectionName)
        {
            // Store the section name
            SectionName = sectionName;

            // Load the application ini
            using (IniFile Ini = new IniFile(FileName, IniPassword))
            {

                // Check if the desired section exists
                if (Ini.SectionExists(sectionName))
                {
                    // Yep, so delete it
                    Ini.EraseSection(sectionName);
                    Ini.Save();
                }
            }
        }