What do you do if your application has default settings but you want the user to be able to change them (for instance, background colors or font settings) and reload them the next time your app starts? You can store them in several places but this topic is about putting them in your application config file.
When you create a C# project, a special file is created for you automatically that holds important runtime information such as the required .NET framework version, database connection strings, and other information. The file name has the format of {your executable name}.exe.config (i.e. GlobalThermoNuclearWar.exe.config). It is put in the same folder as your executable. If this file is ever deleted, the application will use the original values that were stored when the executable was initially compiled.
In the example below, I have created a new C# project and added three settings to the project. Two are in the User scope which allows the user to replace the values in the config file. The last is in the Application scope meaning that the user cannot alter this value because it is maintained as a read-only property. The Type drop down allows you to create many different types of settings properties.
Once the app is rebuilt and started, you can retrieve the Application and User scoped values as properties.
// Read all properties to local values. int settingsInt = Properties.Settings.Default.SettingsInt; string settingsString1 = Properties.Settings.Default.SettingsString1; string settingsApp = Properties.Settings.Default.SettingsApp; // Or just leave them as properties. if (Properties.Settings.Default.SettingsInt > 10) { ... }
While Application scope settings cannot be modified from software, you can modify the User scoped properties as desired. Be aware that if you want to store them for the next time the app is started, be sure to save them.
// Change a setting. Properties.Settings.Default.SettingsInt += 1; // Now save the change for next time. Properties.Settings.Default.Save();
Once the settings are saved, they are written to a .config file at “%localappdata%/{your executable name}/”. This file will only be created if the settings are saved programmatically.
And finally, if you want to reset all settings to the original build settings, all it takes is a single method.
// Restore the original settings and save them to disk. Properties.Settings.Default.Reset();
More information