Search code examples
c#.netwinformsapplication-settings

How to get the current value of a Property Setting at run-time using a loop


I have a Property named A and it was set to the value AAA via the Properties Setting Window in Visual Studio (Project → Properties → Settings).

I can obtain the original value for the property A using this loop.
If I change the value, to say NewValue, I can get the new value using the code:

Properties.Settings.Default.A

However, within the loop, I don't know how to get the current property value without using the syntax:

Properties.Settings.Default.VariableName

for example:

Properties.Settings.Default.A= "NewValue";
Properties.Settings.Default.Save();
foreach (SettingsProperty _currentProperty in Properties.Settings.Default.Properties)
{
    Console.WriteLine(_currentProperty.Name + "," + _currentProperty.DefaultValue.ToString());
}

The above loop shows the original value of the property (old default value which was AAA).

I have checked the user.config file and made sure it is showing NewValue.

I assume that there must be some way to refer to the current value using a property or a method I don't know (maybe I should iterate another collection?).

The question is, how to display this new value inside the foreach loop above?


Solution

  • The current Properties.Settings values are returned in a SettingsPropertyValueCollection Class when querying a SettingsProvider (if no Provider is defined for a setting, the default Provider, LocalFileSettingsProvider, is used).

    The current code is modifying the value of a Setting and then inspect these values after the settings have been saved to the local storage.
    The code iterates the Properties.Settings.Default.Properties collection, corresponding to the ApplicationSettingsBase.Properties property, a SettingsPropertyCollection collection, which contains SettingsProperty objects, used internally to represent the metadata of configuration properties.

    This class can only return the Default value of the associated Setting.

    To inspect the current values associated to Properties Settings, the code should instead iterate the aforementioned SettingsPropertyValueCollection, returned by ApplicationSettingsBase.PropertyValues property, which enumerates the SettingsPropertyValue objects.
    The PropertyValue property of these objects returns the run-time value currently assigned to the Setting.

    To return the current values, the code can then be modified in:

    foreach (SettingsPropertyValue prop in Properties.Settings.Default.PropertyValues) {
        Console.WriteLine($"{prop.Name} -> {prop.PropertyValue}");
    }
    

    A couple of notes about the Application Settings:

    Application Settings are organized in 2 main categories:

    1. Application Settings (Settings in the Application scope)
      Read-only, no local storage is associated, so cannot be changed at run-time. These settings are stored int the app.config files, which is the copied to the [Application].exe.config file when a Project is built.

    2. User Settings (Settings in the User scope) (applies to Visual Studio 2017+)
      These settings are stored along with the Application settings, but have associated local storage files, used to store values that a User (or the Application) can change at run-time:
      2.1 A user.config file stored in the current User profile in the

      [User]/AppData/Local/[CompanyName]/[ProductName].exe_<hash>/[Assembly Version]  
      

      section, if the setting's Roaming attribute is set to false.
      2.2 A user.config file stored in the current User profile in the

      [User]/AppData/Roaming/[CompanyName]/[ProductName]/[File Version]
      

      section, if the setting Roaming attribute is set to true instead.

    The original values of settings in the User scope, the Default values, are not modified when a new value is assigned to the Setting.


    • Settings in the User scope can save new values assigned at run-time, using the ApplicationSettingsBase.Save() method:

      Properties.Settings.Default.Save();
      
    • An application can reload the last saved values (to discard recent changes, maybe), with the ApplicationSettingsBase.Reload() method:

      Properties.Settings.Default.Reload();
      
    • To discard all changes made to the User settings, the Settings can be reset to default values using the ApplicationSettingsBase.Reset() method:

      Properties.Settings.Default.Reset();