Modifying .NET Configuration Files at Runtime

In the previous article "Refreshing the .NET Application to read modified configuration files values", we discussed how to make a Windows Application that reads the modified values instead of reading the values from the Cache.

 
This application should have the capability to change the configuration value of the previous application and when we click on the button in the previous application, it should show us the changed value.
 
Create a new Windows Forms application in the same solution file we used in the previous article.
 
Administration-Console.gif
 
Figure: Windows Form application for Administration Console
 
Add a TextBox and a Button control on the form as below:
 
Controls-added-to-the-form.gif
 
Figure: Controls added to the form
 
Now when the user clicks on the Modify button it should change the configuration value of the other application with the TextBox value. To do this, we need to use some of the settings code supported by the ConfigurationManger class in the nameSpace System.Configuration.
 
Add a System.Configuration reference to the project and change the code in your file Form1.cs as below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Configuration;  
  11. namespace AdminConsole {  
  12.    public partial class Form1: Form {  
  13.        public Form1() {  
  14.            InitializeComponent();  
  15.        }  
  16.        private void button1_Click(object sender, EventArgs e) {  
  17.            //check if textbox is empty. if so ask user to enter value  
  18.            if (textBox1.Text == "")  
  19.                MessageBox.Show("Enter Value");  
  20.            else {  
  21.                //first get the exe file path of the destination application whose configuration settins have been modified  
  22.                Configuration config = ConfigurationManager.OpenExeConfiguratio(@ "C:\WindowsAppSettingRefresh\TestAppSettingRefresh\TestAppSettingRefresh\bin\Release\TestAppSettingRefresh.exe");  
  23.                //now change the element of the settings file with the textbox text  
  24.                config.AppSettings.Settings["LoggingFlag"].Value = textBox1.Text;  
  25.                //now save the configuration file  
  26.                config.Save();  
  27.                MessageBox.Show("Value is changed");  
  28.            }  
  29.        }  
  30.    }  
Now build the solution in Release Mode and now go to the bin\Release folders of both the projects and click on the exe files.
 
Two-applications-running.gif
 
Figure: Two applications running
 
Now click on the "Show Logging Flag" button to see what is the value already set in the configuration file.
 
Initial-value-set.gif
 
Figure: Initial value set in the configuration file
 
Now set the value to be changed in the AdminConsole application and click on the "Modify" button.
 
Changing-the-configuration-element.gif
 
Figure: Changing the configuration element value
 
Now check the value by clicking on the button "Show Logging Flag".
 
Reading-and-displaying-value.gif
 
Figure: Reading and displaying value from the configuration file.
 
We see that the value has been modified in the configuration file and even the value has been successfully read by another application. You can try various values and see if it is shown in the other application.
 
This scenario is useful when the administrator wants to take control of changing the values of a Windows Service configuration file without changing the configuration file manually and restarting the service to read the new value.
 
Hope you liked this article. Have fun with programming!!!