C# Tips

C# Tip Article

Using Application-specific Settings

Application Settings allows you to store and retrieve property settings and other information for your application dynamically. Settings page can be accessed by choosing Project Properties -> Settings tab or double-clicking Properties\Settings.settings in Solution Explorer.

In the Settings.settings, you can define application-scoped settings and/or user-scoped settings as shown below.

Properties Settings.settings

Settings can be accessed from "Properties.Settings.Default" in C#. With "Properties.Settings.Default," application-scoped settings are read-only whereas user-scoped settings are read-write.

When the application is built after Settings are defined in VS designer, application config file (ex: WinFormApp.exe.config) will include userSettings and/or applicationSettings with their default values if any.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
			<section name="WinFormApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
		</sectionGroup>
		<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
			<section name="WinFormApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
		</sectionGroup>
	</configSections>
	<startup> 
		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
	</startup>
	<userSettings>
		<WinFormApp.Properties.Settings>
			<setting name="userData" serializeAs="String">
				<value>USER</value>
			</setting>
		</WinFormApp.Properties.Settings>
	</userSettings>
	<applicationSettings>
		<WinFormApp.Properties.Settings>
			<setting name="appData" serializeAs="String">
				<value>APP</value>
			</setting>
		</WinFormApp.Properties.Settings>
	</applicationSettings>
</configuration>

Any kind of settings can be read by using Properties.Settings.Default.

User-scoped settings can be updated as below. When user-scoped settings are updated, they are saved to user.config file under each user's profile directory. For example, user-scoped settings of WinFormApp.exe were saved to the following folder in my Windows 10 (without roaming profile) : C:\Users\{username}\AppData\Local\WinFormApp\WinFormApp.exe_Url_02z2xff0zxapye5pmgcdih0lygdijlnz\1.0.0.0\user.config

	Properties.Settings.Default.userData = "USR 101";
	Properties.Settings.Default.Save();

Application-scoped settings can be updated programmatically by using ConfigurationManager. When application-scoped settings are updated in C#, the settings will be saved to application config file (ex: WinFormApp.exe.config) in the application folder.

	// using System.Configuration;
	Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

	ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
	ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["WinFormApp.Properties.Settings"];
	ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;

	SettingElement appData = clientSection.Settings.Get("appData");
	appData.Value.ValueXml.InnerText = "APP 101";
	applicationConfigSection.SectionInformation.ForceSave = true;
	config.Save();