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.
Figure: Windows Form application for Administration Console
Add a TextBox and a Button control on the form as below:
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) {
-
- if (textBox1.Text == "")
- MessageBox.Show("Enter Value");
- else {
-
- Configuration config = ConfigurationManager.OpenExeConfiguratio(@ "C:\WindowsAppSettingRefresh\TestAppSettingRefresh\TestAppSettingRefresh\bin\Release\TestAppSettingRefresh.exe");
-
- config.AppSettings.Settings["LoggingFlag"].Value = textBox1.Text;
-
- 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.
Figure: Two applications running
Now click on the "Show Logging Flag" button to see what is the value already set in the configuration file.
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.
Figure: Changing the configuration element value
Now check the value by clicking on the button "Show Logging Flag".
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!!!