Esempio n. 1
0
        /// <summary>
        /// Read PAD settings from INI file.
        /// </summary>
        /// <param name="padSectionName">INI section.</param>
        /// <param name="s">Settings.</param>
        /// <param name="p">PAD Settings.</param>
        /// <remarks>use 'ref' to indicate that objects will be updated inside.</remarks>
        void ReadWritePadSettings(string padSectionName, ref Setting s, ref PadSetting p, bool write)
        {
            s = new Setting();
            p = new PadSetting();
            // Get PAD1 settings data as a reference.
            var items  = SettingsMap.Where(x => x.MapTo == MapTo.Controller1).ToArray();
            var sProps = s.GetType().GetProperties();
            var pProps = p.GetType().GetProperties();
            var ini2   = new Ini(IniFileName);

            foreach (var item in items)
            {
                string key = item.IniPath.Split('\\')[1];
                // Try to find property inside Settings object.
                var sProp = sProps.FirstOrDefault(x => x.Name == item.PropertyName);
                if (sProp != null)
                {
                    if (write)
                    {
                        var sv = (string)sProp.GetValue(s, null) ?? "";
                        // Write value to INI file.
                        ini2.SetValue(padSectionName, key, sv);
                    }
                    else
                    {
                        // Read value from INI file.
                        var sv = ini2.GetValue(padSectionName, key) ?? "";
                        sProp.SetValue(s, sv, null);
                    }
                    continue;
                }
                // Try to find property inside PAD Settings object.
                var pProp = pProps.FirstOrDefault(x => x.Name == item.PropertyName);
                if (pProp != null)
                {
                    if (write)
                    {
                        var pv = (string)pProp.GetValue(p, null) ?? "";
                        // Write value to INI file.
                        ini2.SetValue(padSectionName, key, pv);
                    }
                    else
                    {
                        // Read value from INI file.
                        var pv = ini2.GetValue(padSectionName, key) ?? "";
                        sProp.SetValue(s, pv, null);
                    }
                    continue;
                }
                // Property was not found.
                var message = string.Format("ReadWritePadSettings: Property '{0}' was not found", item.PropertyName);
                // Inform developer with the message.
                MessageBoxForm.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }