/// <summary> /// We're only supporting three sections here at the moment. /// The default appSettings, plus our standard ApplicationConfiguration /// and CommonConfiguration section handlers. These handlers have extended support for /// the Description attribute in addition to the Key, Value pair attributes /// in the Xml configuration file. /// </summary> public void UpdateConfiguration(PropertyGridSettingsComponent customClass, bool commit) { try { //Save a backup version if (!string.IsNullOrEmpty(originalFile)) Xmldoc.Save(this.originalFile + "_bak"); //Populate our property collection. PropertyDescriptorCollection props = customClass.GetProperties(); for (int i = 0; i < props.Count; i++) { PropertyDescriptor item = props[i]; string value = item.GetValue(null).ToString(); string category = item.Category; string key = item.DisplayName; Type type = item.PropertyType; if (type == typeof(System.Drawing.Color)) Settings[category][key].ColorValue = (System.Drawing.Color)item.GetValue(null); else Settings[category][key].Value = value; Settings[category][key].ValueType = type; } Commit(); } catch (Exception ex) { throw ex; } }
public void Update(PropertyGridSettingsComponent compo, bool commit = true) { xmlConfig.UpdateConfiguration(compo, commit); }
/// <summary> /// Load the Xml Configuration document and populate our CustomClass with a dynamic property /// for each of the supported configuration sections. We're only supporting three sections here. /// The default appSettings, plus our standard ApplicationConfiguration /// and CommonConfiguration section handlers. These handlers are derived from IConfigurationSectionHandler. /// They have extended support for the Description attribute in addition to the Key, Value /// pair attributes in the Xml configuration file. /// This could easily be extended to include support for any section under the configuration /// section that has the <add key="value" value="value"/> structure (assuming you haven't written a /// completely new Xml structure for your custom configuration section). /// </summary> public PropertyGridSettingsComponent GetPropertyGridSettingsComponent() { PropertyGridSettingsComponent customClass = new PropertyGridSettingsComponent(); try { XmlDocument xmlDoc = this.Xmldoc; XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes; foreach (XmlNode section in nodes) { XmlNodeList itemList = section.ChildNodes; foreach (XmlNode item in itemList) { object value = string.Empty; if (item.Attributes["value"] != null) value = item.Attributes["value"].Value; Type type = typeof(String); if (item.Attributes["type"] != null) { try { if ("System.Drawing.Color" == item.Attributes["type"].Value) { type = typeof(System.Drawing.Color); value = XmlConfig.ColorFromString(value.ToString()); } else if (typeof(Enum).IsAssignableFrom(Type.GetType(item.Attributes["type"].Value))) { type = Type.GetType(item.Attributes["type"].Value); value = Enum.Parse(type, value.ToString()); } else { type = Type.GetType(item.Attributes["type"].Value); } } catch { type = typeof(String); } if (type == null) type = typeof(String); } customClass.AddProperty(item.Name, value, section.Name + "/" + item.Name + " => " + type.Name, section.Name, type, false, false); } } return customClass; } catch { return null; } }