1

I wanted to know the difference between these 3 Settings.Default.<PropertyName> , Settings.Default.Properties and Settings.Default.PropertyValues .
I have a wpf window that would dynamically generate controls based on these Settings and then one can update the Settings values I first used Settings.Default.Properties Collection , but I believe it does not update the values in either config or the physical settings file in the user folder. So I used reflection to update , But I still couldn't figure how to obtain values by Reflection . (May still need to research on this)

Settings.Default.GetType().GetProperty(propertyName, 
    typeof(string)).SetValue(source, fileDialog.FileName, null);
Settings.Default.Save();
Settings.Default.Reload();

Then I saw Settings.Default.PropertyValues has the latest updated values and tested that in debug mode,

string properyValue = 
Convert.ToString(Settings.Default.PropertyValues[propertyName].PropertyValue);

strangely they don't seem to be working when I created the installer and exe . Still to figure what exactly wrong. Can someone point me out if I am complicating things and missing something?

Update 1

After nflash's comment , I checked when the file was created. The file was not created when the application starts for all 3 , I even called Settings.Default.Save right at the start but it doesn't create the file
Settings.Default.<PropertyName> , Settings.Default.Properties are instantiated but Settings.Default.PropertyValues not.
Only once I make a change in the Settings and Save , the file is created.

Update2

Right now the solution that I came with is

source.GetType().GetProperty(setting.Name, typeof(string))
                .SetValue(source, "NewValue", null);

As mentioned by nflash , it would be type safe (Although Reflection has it's demirits) . But the Settings.Default.<PropertyName> is synchronized and instantiated correctly hence.

Micha Wiedenmann
  • 18,825
  • 20
  • 87
  • 132
Rameez Ahmed Sayad
  • 1,252
  • 6
  • 15
  • 29
  • 1
    Note that for whatever reason, the `PropertyValues` collection is not populated until you retrieve a property value from the `Settings` object. It doesn't matter which property you retrieve...you just need to get one, to force the class to populate the values. See https://stackoverflow.com/questions/89149/c-why-does-settings-propertyvalues-have-0-items – Peter Duniho Aug 01 '19 at 01:23

3 Answers3

1

Just want to add that you can only change settings with "User" scope. When you save the new value of the setting the value is not saved in the config in the application path but instead it is saved in the user.config inside the %localappdata% folder (%localappdata%\CompanyName\ApplicationName_someGUID\AppVersion)

Update: About your last update, the user.config file is only created when you save a setting with a value different from the default value of the setting.

I'm not sure if you still have questions about this, so I'm trying to add more info:

Settings.Default.<PropertyName> as wonko79 pointed out is just a property accessor to the corresponding value. If you look at the code behind the settings (or just Go To Definition of the property) you will see something like this:

public string PropertyName {
        get {
            return ((string)(this["PropertyName"]));
        }
        set {
            this["PropertyName"] = value;
        }
    }

The array operator is accessing the underlying structure that holds the values that in fact is the PropertyValues.

The difference between the Properties and PropertyValues is a little bit trickier. These are really two different structures (one is a SettingsPropertyCollection and the other is a SettingsPropertyValueCollection). The Properties property is defined in the ApplicationSettingsBase class and the PropertyValues is defined in the SettingsBase class.

The SettingsProperty class (elements of the SettingsPropertyCollection) contains information about the setting itself (metadata?) and its default value.

The SettingsPropertyValue class (elements of the SettingsPropertyValueCollection) contains the actual value of the setting and some additional control information, like if the value is dirty, if it is using default value, etc.

In the end of the day this is all how .NET internally manages the Settings and all that we need to know is how to get and set these settings.

I always like to work with the properties that the setting designer generates as it is strongly typed and already makes the cast to the corresponding type. Also using the Properties or PropertyValues requires a string as a parameter and in case of a typo I will only get an error in runtime as opposed to the compile error that I get if misspell the name of the property.

nflash
  • 411
  • 3
  • 16
  • It is in User Scope and will update some findings , that I observe. – Rameez Ahmed Sayad Dec 17 '13 at 10:40
  • I would keep this question up for a while if someone can catch in more info, just another mention both `Properties` and `PropertyValues` are implemented in `ApplicationSettingsBase` and `SettingsBase` is the abstract class. – Rameez Ahmed Sayad Dec 18 '13 at 07:14
  • Actually the PropertyValues in ApplicationSettingsBase just returns base.PropertyValues and the actual implementation of PropertyValues is implemented in SettingsBase. – nflash Dec 18 '13 at 10:07
  • Correct .. Thanks for that ... strange implementation , the member is supposed to be overridden and again calls the base class and in the base class it is assigned in the constructor of the abstract SettingsBase class . probably you could add that since Properties implementation is directly in the abstract class , it's values are available as soon as we invoke , but for PropertyValues implementation is dependent on the dervived class of ApplicationSettingsBase , which I'm yet to figure. – Rameez Ahmed Sayad Dec 19 '13 at 11:25
0

To save the settings after changing them you have to call

Settings.Default.Save();

Settings.Default.<PropertyName> is a property accessor to the corresponding settings value. Settings.Default.Properties is a collection of all settings in your settings file. Settings.Default.PropertyValues is a collection of all values of the settings in your settings file.

Maybe Using Settings in C# is a good starting point to read.

0

The article mentioned by user1064248 is good info, but does not address the PropertyValues issue. I cannot add anything to the good advice from Rameez and nflash except for this pragmatic advice: If you wish to force PropertyValues to populate, you need only to force a value to change. In my settings I have a DateTime property called "Timestamp" and if you put this in the form Load event, you will find that nn1 is zero, and nn2 contains the count all of the properties:

int nn1 = Properties.Settings.Default.PropertyValues.Count;
Properties.Settings.Default.Timestamp = DateTime.UtcNow; // Forces PropertyValues to load
int nn2 = Properties.Settings.Default.PropertyValues.Count;
batpox
  • 306
  • 2
  • 7