private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
{
Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);
#if true // my reimplementation
if (userGroup == null) {
userGroup = new UserSettingsGroup ();
config.SectionGroups.Add ("userSettings", userGroup);
ApplicationSettingsBase asb = context.CurrentSettings;
ClientSettingsSection cs = new ClientSettingsSection ();
userGroup.Sections.Add ((asb != null ? asb.GetType () : typeof (ApplicationSettingsBase)).FullName, cs);
}
bool hasChanges = false;
foreach (ConfigurationSection section in userGroup.Sections) {
ClientSettingsSection userSection = section as ClientSettingsSection;
if (userSection == null)
continue;
foreach (SettingsPropertyValue value in collection) {
if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
continue;
hasChanges = true;
SettingElement element = userSection.Settings.Get (value.Name);
if (element == null) {
element = new SettingElement (value.Name, value.Property.SerializeAs);
userSection.Settings.Add (element);
}
if (element.Value.ValueXml == null)
element.Value.ValueXml = new XmlDocument ().CreateElement ("value");
switch (value.Property.SerializeAs) {
case SettingsSerializeAs.Xml:
element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
break;
case SettingsSerializeAs.String:
element.Value.ValueXml.InnerText = value.SerializedValue as string;
break;
case SettingsSerializeAs.Binary:
element.Value.ValueXml.InnerText = value.SerializedValue != null ? Convert.ToBase64String (value.SerializedValue as byte []) : string.Empty;
break;
default:
throw new NotImplementedException ();
}
}
}
if (hasChanges)
config.Save (ConfigurationSaveMode.Minimal, true);
#else // original impl. - likely buggy to miss some properties to save
foreach (ConfigurationSection configSection in userGroup.Sections)
{
ClientSettingsSection userSection = configSection as ClientSettingsSection;
if (userSection != null)
{
/*
userSection.Settings.Clear();
foreach (SettingsPropertyValue propertyValue in collection)
{
if (propertyValue.IsDirty)
{
SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
element.Value.ValueXml = new XmlDocument();
element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
userSection.Settings.Add(element);
}
}
*/
foreach (SettingElement element in userSection.Settings)
{
if (collection [element.Name] != null) {
if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
continue;
element.SerializeAs = SettingsSerializeAs.String;
element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue; ///Value = XmlElement
}
}
}
}
config.Save (ConfigurationSaveMode.Minimal, true);
#endif
}