Wednesday 26 August 2015

Programmatically Change Values in Configuration File During Runtime in C# & VB

In this post, I will show you guys on how to programmatically change the values defined in configuration file. [Thanks to Serena Yeoh for introduce this to me.]

"Be aware that it is not recommended to change the value in web application's configuration file, this is due to changing of the configuration file will cause the application pool to restart. What this means is that the value kept in session or in static will be lost."

Well, don't be scared off by the statement. It doesn't mean that you can't use it in different environment. There might be a situation that requires you to change the value in configuration file, such as the value defined in configuration file changes the way how your application behaves and you want to test these behaviors in your Unit Test. Changing the configuration value manually and re-run the test can be very tedious and other developers that are sharing/developing the same project might not be aware about this.

To deal with this kind of situation, you will need to change them in code. So let's get started.

You need to have an instance of Configuration. By calling the following code, it will retrieve the current project's configuration file (app.config).

[C#]
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

[VB]
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

If your application is using a custom configuration and wants to change the value in the custom configuration, you will need to get the instance of the section. Assuming my section's name is "sampleSectionGroup/changeConfiguration".

[C#]
var configSection = (ChangeConfigurationSection)config.GetSection("sampleSectionGroup/changeConfiguration");

[VB]
Dim configSection = CType(config.GetSection("sampleSectionGroup/changeConfiguration"), ChangeConfigurationSection)

With the configuration retrieved, you can then change the configuration value. The following code is used to change the value in appSettings. Assuming my appSettings contain keys with the name "sampleAppKeyOne" and "sampleAppKeyTwo".

[C#]
config.AppSettings.Settings["sampleAppKeyOne"].Value = "NewSampleAppKeyOne";
config.AppSettings.Settings["sampleAppKeyTwo"].Value = "NewSampleAppKeyTwo";

[VB]
config.AppSettings.Settings("sampleAppKeyOne").Value = "NewSampleAppKeyOne"
config.AppSettings.Settings("sampleAppKeyTwo").Value = "NewSampleAppKeyTwo"

The following code is used to change the value in a custom made configuration. Assuming my element name is "sampleConfig" that contain 2 string attribute "samplePropertyOne" and "samplePropertyTwo".

[C#]
configSection.SampleConfig.SamplePropertyOne = "NewSamplePropertyOne";
configSection.SampleConfig.SamplePropertyTwo = "NewSamplePropertyTwo";

[VB]
configSection.SampleConfig.SamplePropertyOne = "NewSamplePropertyOne"
configSection.SampleConfig.SamplePropertyTwo = "NewSamplePropertyTwo"

Once you have done changing the value. You need to save them back to the configuration file.

[C#]
config.Save(ConfigurationSaveMode.Modified);

[VB]
config.Save(ConfigurationSaveMode.Modified)

If you have the need to access the updated value from the configuration file immediately after the changes is saved to the configuration file. you need to refresh the section that you have changed. So if let's say the appSettings and the custom configuration's value is changed and need to get the updated value from the configuration file. those section needs to be refreshed by calling the following code.

[C#]
ConfigurationManager.RefreshSection("appSettings");
ConfigurationManager.RefreshSection("sampleSectionGroup/changeConfiguration");

[VB]
ConfigurationManager.RefreshSection("appSettings")
ConfigurationManager.RefreshSection("sampleSectionGroup/changeConfiguration")

The sample code can be obtained here https://onedrive.live.com/redir?resid=E6612168B803803D!331&authkey=!AGahxzzrDJsW3MM&ithint=file%2czip




No comments:

Post a Comment