I created a way to dynamically add SettingsProperty
to a .NET app.config
file. It all works nicely, but when I am launching my app the next time I can only see the properties that are created in the designer. How can I load back the properties runtime?
My code for creating the SettingsProperty
looks like the following:
internal void CreateProperty<T>(string propertyName)
{
string providerName = "LocalFileSettingsProvider";
System.Configuration.SettingsAttributeDictionary attributes = new SettingsAttributeDictionary();
System.Configuration.UserScopedSettingAttribute attr = new UserScopedSettingAttribute();
attributes.Add(attr.TypeId, attr);
System.Configuration.SettingsProperty prop;
SettingsProvider provider = ApplicationEnvironment.GlobalSettings.Providers[providerName];
prop = new System.Configuration.SettingsProperty(
propertyName,
typeof(T),
provider,
false,
default(T),
System.Configuration.SettingsSerializeAs.String,
attributes,
false,
false
);
ApplicationEnvironment.GlobalSettings.Properties.Add(prop);
ApplicationEnvironment.GlobalSettings.Reload();
}
When the next run I ask for the settings property I could not find any of the properties created previosuly. No matter if I call the ApplicationEnvironment.GlobalSettings.Reload();
or not.
User defined configuration settings are tied to the assembly version they were created with. If you have a rolling version number (1.0.. for example), You will lose the settings from the last run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With