Reputation: 612
I need to deploy a Windows Forms application using ClickOnce deployment. (VS2008, .NET 3.5) And I need to provide a configuration file for this app that any user can modify. For this reason, I am using Application Settings instead of standard appSetttings in app.config so I can separate the the user config from app config.
see http://msdn.microsoft.com/en-us/library/ms228995(VS.80).aspx
Creating a Settings.settings file using VS generated a class with hard-coded default values like this:
[global::System.Configuration.DefaultSettingValueAttribute("blahblah")]
public string MyProperty
...
I want to read the default values from the app.config!
So I created my own class deriving from ApplicationSettingsBase but I cannot get this to read values from the app.config. Any ideas?
Upvotes: 5
Views: 2673
Reputation: 612
I implemented ApplicationSettingsBase as follows:
public class UserSettings : ApplicationSettingsBase
{
private static UserSettings defaultInstance = ((UserSettings)(ApplicationSettingsBase.Synchronized(new UserSettings())));
public static UserSettings Default
{
get
{
return defaultInstance;
}
}
[UserScopedSetting()]
public string MyProperty
{
get { return (string)this["MyProperty"]; }
set { this["MyProperty"] = (string)value; }
}
//add more properties
}
And added correct xml in app.config...
see http://msdn.microsoft.com/en-us/library/8eyb2ct1(VS.80).aspx
and it works. HTH!
A word of warning!! The ApplicationSettingsBase appears to use some lazy-loading in the implementation. The ApplicationSettingsBase.Properties
and ApplicationSettingsBase.PropertyValues
collections remain empty until at least one property is accessed.
UserSettings settings = new UserSettings();
string temp = settings.MyProperty;//without this line, settings.PropertyValues is empty!!
SettingsPropertyValueCollection properties = settings.PropertyValues;
Upvotes: 0