Dynamic AppSettings C# Example

AppSettings – Dynamically Adding & Updating

1. Introduction to AppSettings

In the last article, we saw how to use the app.config file, its hierarchy, and preference in picking the App settings. In this article, we will see updating the app.config file at runtime. The article example shows adding, deleting and updating a Key-Value entry in the appSettings Section of app.config file. Note, all the settings related to the App goes under the appSettings section. Have a look at the below screen-shot taken from the app.config file:

Sample Config File Settings
Sample Config File Settings

The boxed item is one pair of setting; say for an example, an application by giving the key Key_4 can get the setting value value_4. In this example, we will see how to add, delete and update the settings that come under ‘appSettings Section’ of the config file.

2. About The Example

The screenshot below shows the Sample App we will develop in this Example:

Dynamic AppSettings C# Example
Dynamic AppSettings C# Example

In the above example, the result of the action performed on the App.config file is passed on to the user through the ‘Display Area’. To add (Button Marked as 5) a setting to the app.config file, the key and value should be keyed-in in the Setting Key, Setting Value text boxes marked as 2 and 3 in the screenshot. The user should mention the key in the Setting Key text box to get (Marker 6) the AppSetting entry from the ‘App.Config’. We show the retrieved key in the display area and in the Setting value. To adjust (Through button marker 7) an existing setting, the user will point out the existing key and changed value in the Setting Key and Setting Value, respectively. To delete (Marker 8) a key, the user will specify it in the Setting Key. Note we do all these actions at run time.

3. Adding AppSettings Entry

3.1 Check If Key-Vale Exists in App.Config <AppSettings>

To add a new AppSettings entry, first we should make sure that the entry does not exist in the App.Config. In the below code, we open the config file by making a call to OpenExeConfiguration() function. Note, here we pass the executable filename as an argument to it. Then we get the ‘ KeyValueConfigurationCollection’ object from the config file by calling the GetSection() function.

The reference stored in app_settings now point out the entire Key-Value pair of the entries which present inside the <appSettings>. To get an entry in the <appSettings> section, the key name is given to the KeyValueConfigurationCollection. In our case, the value given in the ‘Setting Key’ text box is handed over to the app_settings to get a single pair that present in the <AppSettings> and if the key does not exist, a null is returned instead of a valid KeyValueConfigurationElement object. Below is the code which checks the key already exists:

3.2 Save Key-Value Pair to <AppSettings>

Once we make sure that the setting key does not exist in the AppSettings section, we create construct a new Key-Value pair object KeyValueConfigurationElement  and add it to the KeyValueConfigurationCollection. Once the collection is updated with the recent entry, we save it to the App.Config file by calling the save() function on the Configuration object. Below is the code:

Apart from this, rest of the code is written to show the result of the Action (Adding a Config. entry). Below is full code for adding an AppSettings config entry:

4. Reading AppSettings Entry

In the past section, we added a config entry dynamically and now we will get the element from the config file. The code below retrieves the newly added application entry without re-launching the application. Here we check that the key entry already exists in the app.config file and if so, we read and show the value in the Display Area.

5. Changing AppSettings Entry

We can change the Key-Value pair in the application configuration indirectly. First, we will grab the Key-Value pair, then remove it. After dropping the entry, we add the fresh one with the same Key Name. The below code is searching for a specific key & drops it. Then, we push a recent entry with the same key name and changed value into the AppSettings section. As the code is like what we learned already, we do not need a further code explanation here. Below is the complete code for modifying the configuration entry:

6. Deleting AppSettings Entry

By this time, we are aware of how to do this. Yes, like the previous section of code, after dropping the configuration section entry, we save the configuration file instead of creating an entry with the same key name.

The Video shows how the Example application works:

Video: Running The Example – Manipulate AppSettings At Runtime

Source Code : Download Dynamic AppSettings Manipulation C# Example From Google Drive

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.