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:
 
 
using System;
 
using System.Collections.Generic;
 
using System.ComponentModel;
 
using System.Data;
 
using System.Drawing;
 
using System.Linq;
 
using System.Text;
 
using System.Threading.Tasks;
 
using System.Windows.Forms;
 
using System.Configuration; 
 
namespace AdminConsole
 {
    
public partial class Form1 : Form
 
    {
        
public Form1()
         {
             InitializeComponent();
         }
        
private void button1_Click(object sender, EventArgs e)
         {
            
//check if textbox is empty. if so ask user to enter value
 
            if (textBox1.Text == "")
                
MessageBox.Show("Enter Value");
            
else
 
            {
                
//first get the exe file path of the destination application whose configuration settins have been modified
 
                Configuration config = ConfigurationManager.OpenExeConfiguratio
 (
@"C:\WindowsAppSettingRefresh\TestAppSettingRefresh\TestAppSettingRefresh\bin\Release\TestAppSettingRefresh.exe");
                
//now change the element of the settings file with the textbox text
 
                config.AppSettings.Settings["LoggingFlag"].Value = textBox1.Text;
 
                //now save the configuration file
 
                config.Save();
                
MessageBox.Show("Value is changed");
             }              
         }
     }
 }
 

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!!!

Up Next
    Ebook Download
    View all
    Learn
    View all