/// <summary> /// Constructor. /// </summary> public SettingsPropertyDescriptor(string name, string typeName, string description, SettingScope scope, string defaultValue) { _name = name; _typeName = typeName; _description = description; _scope = scope; _defaultValue = defaultValue; }
public string GetValue(string section, string entry, SettingScope scope) { if (document == null) return null; XmlElement root = document.DocumentElement; if (root == null) return null; XmlNode entryNode; if (scope == SettingScope.User) entryNode = root.SelectSingleNode(GetSectionPath(section) + "/" + GetScopePath(scope.ToString()) + "/" + GetUserPath(Environment.UserName) + "/" + GetEntryPath(entry)); else entryNode = root.SelectSingleNode(GetSectionPath(section) + "/" + GetScopePath(scope.ToString()) + "/" + GetEntryPath(entry)); if (entryNode == null) return null; return entryNode.InnerText; }
public SettingsEntry(ISettingsEntryHost host, XmlElement element) : this(host) { if (element == null) throw new ArgumentNullException("element"); description = element.GetAttribute("Description"); if (!bool.TryParse(element.GetAttribute("GenerateDefaultValueInCode"), out generateDefaultValueInCode)) generateDefaultValueInCode = GenerateDefaultValueInCodeDefault; name = element.GetAttribute("Name"); provider = element.GetAttribute("Provider"); bool.TryParse(element.GetAttribute("Roaming"), out roaming); if ("Application".Equals(element.GetAttribute("Scope"), StringComparison.OrdinalIgnoreCase)) { scope = SettingScope.Application; } else { scope = SettingScope.User; } type = GetType(element.GetAttribute("Type")); this.SerializedValue = element["Value"].InnerText; }
/// <summary> /// Gets the collection of properties that represent settings of the specified scope. /// </summary> /// <param name="settingsClass"></param> /// <param name="scope"></param> /// <returns></returns> public static PropertyInfo[] GetSettingsProperties(Type settingsClass, SettingScope scope) { return GetSettingsProperties(settingsClass).Where(property => GetScope(property) == scope).ToArray(); }
/// <summary> /// Constructor which configures the scope and the default value of the annotated setting. /// </summary> /// <param name="settingScope">The scope the annotated setting should be contained in.</param> /// <param name="defaultValue">Default value this setting will get if the value can not be /// loaded.</param> public SettingAttribute(SettingScope settingScope, object defaultValue) : this(settingScope) { _defaultValue = defaultValue; _hasDefault = true; }
/// <summary> /// Constructor which configures the scope of the annotated setting. /// </summary> /// <param name="settingScope">The scope the annotated setting should be contained in.</param> public SettingAttribute(SettingScope settingScope) { _settingScope = settingScope; }
/// <summary> /// Adds a new <see cref="CategorizedSettingsElement"/> object if one does not exist. /// </summary> /// <param name="name">Name of the <see cref="CategorizedSettingsElement"/> object.</param> /// <param name="value">Value of the <see cref="CategorizedSettingsElement"/> object.</param> /// <param name="description">Description of the <see cref="CategorizedSettingsElement"/> object.</param> /// <param name="encryptValue">true if the Value of <see cref="CategorizedSettingsElement"/> object is to be encrypted; otherwise false.</param> /// <param name="scope">One of the <see cref="SettingScope"/> values.</param> public void Add(string name, object value, string description, bool encryptValue, SettingScope scope) { if ((object)base.BaseGet(name) == null) { // Add the element only if it does not exist. CategorizedSettingsElement setting = new CategorizedSettingsElement(this, name); setting.Update(value, description, encryptValue, scope); Add(setting); } }
public static ICollection<PropertyInfo> GetSettingsProperties(Type settingsClass, SettingScope scope) { return CollectionUtils.Select(GetSettingsProperties(settingsClass), property => GetScope(property) == scope); }
public Setting(SavedSetting savedSetting, SettingScope scope) { _SavedSetting = savedSetting ?? throw new ArgumentNullException(nameof(savedSetting)); Scope = scope; }
public IDictionary <string, string> GetSettingsValues(Type settingsClass, SettingScope scope) { return(GetSettingsValues(new ConfigurationSectionPath(settingsClass, scope))); }
private static ICollection<PropertyInfo> GetProperties(Type settingsClass, SettingScope scope) { return SettingsClassMetaDataReader.GetSettingsProperties(settingsClass, scope); }
/// <summary> /// Serialize public properties of a Settings object to a given xml file /// </summary> /// <param name = "obj">Setting Object to serialize</param> public static void Serialize(object obj) { string fileName = ""; INamedSettings namedSettings = obj as INamedSettings; if (namedSettings != null) { fileName = obj + "." + namedSettings.Name + ".xml"; } else { fileName = obj + ".xml"; } var options = (ServiceLocator.Current.GetInstance(typeof(ISettingsManager)) as ISettingsManager).GetOptions; var log = (ServiceLocator.Current.GetInstance(typeof(ILogger)) as ILogger).GetLogger; log.Trace($"Serialize({ obj},{fileName})"); var globalSettingsList = new Dictionary <string, string>(); var userSettingsList = new Dictionary <string, string>(); var xmlWriter = new XmlSettingsProvider(fileName); var fullFileName = $@"{options.ConfigDir}\{fileName}"; bool isFirstSave = (!File.Exists(fullFileName)); foreach (var property in obj.GetType().GetProperties()) { var thisType = property.PropertyType; var defaultval = ""; #region CLR Typed property if (isCLRType(thisType)) { object[] attributes = property.GetCustomAttributes(typeof(SettingAttribute), false); SettingScope scope; if (attributes.Length != 0) { var attribute = (SettingAttribute)attributes[0]; scope = attribute.SettingScope; defaultval = attribute.DefaultValue; } else { scope = SettingScope.Global; defaultval = ""; } string value = defaultval; if (!isFirstSave) //else default value will be used if it exists { if (obj.GetType().GetProperty(property.Name).GetValue(obj, null) != null) { value = obj.GetType().GetProperty(property.Name).GetValue(obj, null).ToString(); } if (scope == SettingScope.User) { userSettingsList.Add(property.Name, value); } else { globalSettingsList.Add(property.Name, value); } } else { if (scope == SettingScope.Global) { globalSettingsList.Add(property.Name, value); } if (scope == SettingScope.User) { userSettingsList.Add(property.Name, value); } } } #endregion #region not CLR Typed property else { XmlSerializer xmlSerial = new XmlSerializer(thisType); StringBuilder sb = new StringBuilder(); StringWriter strWriter = new StringWriter(sb); XmlTextWriter writer = new XmlNoNamespaceWriter(strWriter); writer.Formatting = Formatting.Indented; object propertyValue = obj.GetType().GetProperty(property.Name).GetValue(obj, null); xmlSerial.Serialize(writer, propertyValue); strWriter.Close(); strWriter.Dispose(); // remove unneeded encoding tag sb.Remove(0, 41); object[] attributes = property.GetCustomAttributes(typeof(SettingAttribute), false); SettingScope scope = SettingScope.Global; if (attributes.Length != 0) { SettingAttribute attribute = (SettingAttribute)attributes[0]; scope = attribute.SettingScope; defaultval = attribute.DefaultValue; } else { scope = SettingScope.Global; defaultval = ""; } string value = defaultval; /// a changer if (!isFirstSave || defaultval == "") { value = sb.ToString(); } if (scope == SettingScope.User) { userSettingsList.Add(property.Name, value); } else { globalSettingsList.Add(property.Name, value); } } #endregion } #region write Settings // write settings to xml foreach (KeyValuePair <string, string> pair in globalSettingsList) { xmlWriter.SetValue(obj.ToString(), pair.Key, pair.Value, SettingScope.Global); } foreach (KeyValuePair <string, string> pair in userSettingsList) { xmlWriter.SetValue(obj.ToString(), pair.Key, pair.Value, SettingScope.User); } xmlWriter.Save(); #endregion }
/// <summary> /// De-serialize public properties of a Settings object from a given xml file /// </summary> /// <param name = "obj">Setting Object to retrieve</param> /// <param name = "fileName">Xml file name</param> public static void Deserialize(object obj) { string fileName = ""; INamedSettings namedSettings = obj as INamedSettings; if (namedSettings != null) { fileName = obj + "." + namedSettings.Name + ".xml"; } else { fileName = obj + ".xml"; } XmlSettingsProvider xmlreader = new XmlSettingsProvider(fileName); var options = (ServiceLocator.Current.GetInstance(typeof(ISettingsManager)) as ISettingsManager).GetOptions; var log = (ServiceLocator.Current.GetInstance(typeof(ILogger)) as ILogger).GetLogger; log.Trace($"Deserialize({obj},{fileName})"); // if xml file doesn't exist yet then create it string fullFileName = String.Format(@"{0}\{1}", options.ConfigDir, fileName); ; if (!File.Exists(fullFileName)) { Serialize(obj); } foreach (PropertyInfo property in obj.GetType().GetProperties()) { Type thisType = property.PropertyType; #region get scope SettingScope scope = SettingScope.Global; object[] attributes = property.GetCustomAttributes(typeof(SettingAttribute), false); string defaultval = ""; if (attributes.Length != 0) { SettingAttribute attribute = (SettingAttribute)attributes[0]; scope = attribute.SettingScope; defaultval = attribute.DefaultValue; } else { scope = SettingScope.Global; defaultval = ""; } #endregion if (isCLRType(thisType)) #region CLR Typed property { try { string value = xmlreader.GetValue(obj.ToString(), property.Name, scope); if (string.IsNullOrEmpty(value)) { value = defaultval; } if (thisType == typeof(string)) { property.SetValue(obj, value, null); } if (thisType == typeof(bool)) { property.SetValue(obj, bool.Parse(value), null); } if (thisType == typeof(Int16)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32)) { property.SetValue(obj, Int32.Parse(value), null); } if (thisType == typeof(Int64)) { property.SetValue(obj, Int64.Parse(value), null); } if (thisType == typeof(UInt16)) { property.SetValue(obj, UInt16.Parse(value), null); } if (thisType == typeof(UInt32)) { property.SetValue(obj, UInt32.Parse(value), null); } if (thisType == typeof(UInt64)) { property.SetValue(obj, UInt64.Parse(value), null); } if (thisType == typeof(float)) { property.SetValue(obj, float.Parse(value), null); } if (thisType == typeof(double)) { property.SetValue(obj, double.Parse(value), null); } if (thisType == typeof(Int16)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32)) { property.SetValue(obj, Int32.Parse(value), null); } if (thisType == typeof(DateTime)) { property.SetValue(obj, DateTime.Parse(value), null); } if (thisType == typeof(bool?)) { property.SetValue(obj, bool.Parse(value), null); } if (thisType == typeof(Int16?)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32?)) { property.SetValue(obj, Int32.Parse(value), null); } if (thisType == typeof(Int64?)) { property.SetValue(obj, Int64.Parse(value), null); } if (thisType == typeof(UInt16?)) { property.SetValue(obj, UInt16.Parse(value), null); } if (thisType == typeof(UInt32?)) { property.SetValue(obj, UInt32.Parse(value), null); } if (thisType == typeof(UInt64?)) { property.SetValue(obj, UInt64.Parse(value), null); } if (thisType == typeof(float?)) { property.SetValue(obj, float.Parse(value), null); } if (thisType == typeof(double?)) { property.SetValue(obj, double.Parse(value), null); } if (thisType == typeof(Int16?)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32?)) { property.SetValue(obj, Int32.Parse(value), null); } } catch (Exception) { // ignored } } #endregion else #region not CLR Typed property { XmlSerializer xmlSerial = new XmlSerializer(thisType); string value = xmlreader.GetValue(obj.ToString(), property.Name, scope); if (value != null) { TextReader reader = new StringReader(value); try { property.SetValue(obj, xmlSerial.Deserialize(reader), null); } catch (Exception) { // ignored } } } #endregion } }
public StringSetting(string path, SettingScope scope, string defaultValue, bool allowEmptyOrWhiteSpace = true) : base(path, scope, defaultValue, StringSettingConverter.Instance) { this.allowEmptyOrWhiteSpace = allowEmptyOrWhiteSpace; }
public IDictionary<string, string> GetSettingsValues(Type settingsClass, SettingScope scope) { return GetSettingsValues(new ConfigurationSectionPath(settingsClass, scope)); }
public static void RemoveSettingsValues(this SystemConfiguration configuration, Type settingsClass, SettingScope? scope = null) { var removeApplicationSettings = !scope.HasValue || scope.Value == SettingScope.Application; var removeUserSettings = !scope.HasValue || scope.Value == SettingScope.User; if (removeApplicationSettings) { var sectionPath = new ConfigurationSectionPath(settingsClass, SettingScope.Application); ConfigurationSectionGroup group = configuration.GetSectionGroup(sectionPath.GroupPath); if (group != null) group.Sections.Remove(sectionPath.SectionName); } if (removeUserSettings) { var sectionPath = new ConfigurationSectionPath(settingsClass, SettingScope.User); var group = configuration.GetSectionGroup(sectionPath.GroupPath); if (group != null) group.Sections.Remove(sectionPath.SectionName); } configuration.Save(ConfigurationSaveMode.Minimal, true); }
public void SetValue(string section, string entry, string value, SettingScope scope) { // If the value is null, remove the entry if (value == null) { RemoveEntry(section, entry); return; } string valueString = value; if (document.DocumentElement == null) { XmlElement node = document.CreateElement("Configuration"); document.AppendChild(node); } XmlElement root = document.DocumentElement; // Get the section element and add it if it's not there XmlNode sectionNode = root.SelectSingleNode("Section[@name=\"" + section + "\"]"); if (sectionNode == null) { XmlElement element = document.CreateElement("Section"); XmlAttribute attribute = document.CreateAttribute("name"); attribute.Value = section; element.Attributes.Append(attribute); sectionNode = root.AppendChild(element); } // Get the section element and add it if it's not there XmlNode scopeSectionNode = sectionNode.SelectSingleNode("Scope[@value=\"" + scope + "\"]"); if (scopeSectionNode == null) { XmlElement element = document.CreateElement("Scope"); XmlAttribute attribute = document.CreateAttribute("value"); attribute.Value = scope.ToString(); element.Attributes.Append(attribute); scopeSectionNode = sectionNode.AppendChild(element); } if (scope == SettingScope.User) { XmlNode userNode = scopeSectionNode.SelectSingleNode("User[@name=\"" + Environment.UserName + "\"]"); if (userNode == null) { XmlElement element = document.CreateElement("User"); XmlAttribute attribute = document.CreateAttribute("name"); attribute.Value = Environment.UserName; element.Attributes.Append(attribute); userNode = scopeSectionNode.AppendChild(element); } } // Get the entry element and add it if it's not there XmlNode entryNode; if (scope == SettingScope.User) { XmlNode userNode = scopeSectionNode.SelectSingleNode("User[@name=\"" + Environment.UserName + "\"]"); entryNode = userNode.SelectSingleNode("Setting[@name=\"" + entry + "\"]"); } else entryNode = scopeSectionNode.SelectSingleNode("Setting[@name=\"" + entry + "\"]"); if (entryNode == null) { XmlElement element = document.CreateElement("Setting"); XmlAttribute attribute = document.CreateAttribute("name"); attribute.Value = entry; element.Attributes.Append(attribute); if (scope == SettingScope.Global) entryNode = scopeSectionNode.AppendChild(element); else { XmlNode userNode = scopeSectionNode.SelectSingleNode("User[@name=\"" + Environment.UserName + "\"]"); entryNode = userNode.AppendChild(element); } } entryNode.InnerText = valueString; modified = true; }
/// <summary> /// Gets the attribute by scope. /// </summary> /// <param name="scope">The scope.</param> /// <returns>The attribute.</returns> private static Type GetAttributeTypeByScope(SettingScope scope) { return(scope == SettingScope.User ? typeof(UserScopedSettingAttribute) : typeof(ApplicationScopedSettingAttribute)); }
/// <summary> /// Gets only those settings values that are different from the defaults for the given settings group. /// </summary> /// <param name="configuration">the configuration where the values will be taken from</param> /// <param name="settingsClass">the settings class for which to get the values</param> /// <param name="settingScope">the scope of the settings for which to get the values</param> public static Dictionary<string, string> GetSettingsValues(this SystemConfiguration configuration, Type settingsClass, SettingScope settingScope) { var properties = GetProperties(settingsClass, settingScope); var sectionPath = new ConfigurationSectionPath(settingsClass, settingScope); return GetSettingsValues(configuration, sectionPath, properties); }
/// <summary> /// Constructor /// </summary> /// <param name = "settingScope">Setting's scope</param> /// <param name = "defaultValue">Default value</param> public SettingAttribute(SettingScope settingScope, string defaultValue) { SettingScope = settingScope; DefaultValue = defaultValue; }
void RegisterAppConfigSectionInGroup(XElement sectionGroup, SettingScope scope) { if (!sectionGroup.Elements("section").Any(e => (string)e.Attribute("name") == setDoc.GeneratedFullClassName)) { XElement section = new XElement("section", new XAttribute("name", setDoc.GeneratedFullClassName)); section.Add(new XAttribute("type", "System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")); if (scope == SettingScope.User) { section.Add(new XAttribute("allowExeDefinition", "MachineToLocalUser")); } section.Add(new XAttribute("requirePermission", false)); sectionGroup.AddWithIndentation(section); } }