Using application parameters and reading default values ​​from app.config

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 application that any user can change. For this reason, I use Application Settings instead of the standard appSetttings in app.config, so I can separate the user configuration from the application configuration.

see http://msdn.microsoft.com/en-us/library/ms228995(VS.80).aspx

Creating a Settings.settings file using VS generates a class with hard-coded default values, for example:

[global::System.Configuration.DefaultSettingValueAttribute("blahblah")]
public string MyProperty
...

I want to read the default values ​​from app.config!

So, I created my own class derived from ApplicationSettingsBase, but I cannot get this to read the values ​​from app.config. Any ideas?

+5
source share
2 answers

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 the correct xml to app.config ...

see http://msdn.microsoft.com/en-us/library/8eyb2ct1(VS.80).aspx

and it works. NTN!

Warning! The ApplicationSettingsBase seems to use some lazy loading in the implementation. Collections ApplicationSettingsBase.Propertiesand ApplicationSettingsBase.PropertyValuesremain empty until it is available at least one property.

UserSettings settings = new UserSettings();
string temp = settings.MyProperty;//without this line, settings.PropertyValues is empty!! 
SettingsPropertyValueCollection properties = settings.PropertyValues;
0
source

, . , , - , ClickOnce, -, ClickOnce. . LocalApplicationData ( Vista Win7), MyDocuments, , .

http://robindotnet.wordpress.com/2009/08/19/where-do-i-put-my-data-to-keep-it-safe-from-clickonce-updates/

RobinDotNet

+1

All Articles