コード例 #1
0
        public void Load(string fileName)
        {
            FileName = fileName;

            if (File.Exists(fileName))
            {
                var contents        = File.ReadAllText(fileName, Encoding.UTF8);
                var jObjectSettings = JArray.Parse(contents);


                Items.Clear();

                SuspendAutoSave();

                foreach (JObject jObjectSettingsItem in jObjectSettings)
                {
                    try
                    {
                        var name = JObjectHelper.GetString(jObjectSettingsItem, "name", string.Empty);
                        var type = JObjectHelper.GetString(jObjectSettingsItem, "type", string.Empty);

                        object value = null;

                        if (type == "string")
                        {
                            value = JObjectHelper.GetString(jObjectSettingsItem, "value", string.Empty);
                        }
                        else if (type == "integer")
                        {
                            value = JObjectHelper.GetInt32(jObjectSettingsItem, "value", 0);
                        }
                        else if (type == "double")
                        {
                            value = JObjectHelper.GetDecimal(jObjectSettingsItem, "value", 0);
                        }
                        else if (type == "boolean")
                        {
                            value = JObjectHelper.GetBoolean(jObjectSettingsItem, "value", false);
                        }
                        else if (type == "guid")
                        {
                            value = JObjectHelper.GetGuid(jObjectSettingsItem, "value", Guid.Empty);
                        }
                        else if (type == "point")
                        {
                            var x = JObjectHelper.GetInt32(jObjectSettingsItem, "x", 0);
                            var y = JObjectHelper.GetInt32(jObjectSettingsItem, "y", 0);
                            value = new Point(x, y);
                        }
                        else if (type == "size")
                        {
                            var width  = JObjectHelper.GetInt32(jObjectSettingsItem, "width", 0);
                            var height = JObjectHelper.GetInt32(jObjectSettingsItem, "height", 0);
                            value = new Size(width, height);
                        }
                        else if (type == "datetime")
                        {
                            var valueStr = JObjectHelper.GetString(jObjectSettingsItem, "value", DateTime.UtcNow.ToString("o"));
                            value = DateTime.Parse(valueStr);
                        }
                        else if (type == "version")
                        {
                            value = new Version(JObjectHelper.GetString(jObjectSettingsItem, "value"));
                        }
                        else if (type == "list:String")
                        {
                            value = new List <string>();
                            var jArrayStrings = (JArray)jObjectSettingsItem["value"];
                            foreach (string str in jArrayStrings)
                            {
                                ((List <string>)value).Add(str);
                            }
                        }

                        var settingsItem = new SettingsItem(name);
                        settingsItem.OnChange += settingsItem_OnChange;
                        settingsItem.Value     = value;
                        Items.Add(settingsItem);
                    }
                    catch (Exception ex)
                    {
                    }
                }

                ResumeAutoSave();
            }
        }