'To update the applications config file - Start
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
MessageBox.Show("App config value before update: " & Convert.ToString(config.AppSettings.Settings("MySample")))
If IsNothing(config.AppSettings.Settings("MySample")) Then
config.AppSettings.Settings.Add("MySample", "FreshlyAddedValue")
config.Save(ConfigurationSaveMode.Modified)
Else
config.AppSettings.Settings("MySample").Value = TextBox1.Text.Trim
config.Save(ConfigurationSaveMode.Modified)
End If
MessageBox.Show("App config value after update: " & Convert.ToString(config.AppSettings.Settings("MySample").value))
'To update the applications config file - End
'To update the config files which doesnot belong to the application - Start
Dim anotherConfigFile As String = "app1.config" 'name of config file. It should be in same path as exe
Dim configFileMap As ExeConfigurationFileMap = New ExeConfigurationFileMap()
configFileMap.ExeConfigFilename = anotherConfigFile
Dim anotherConfig As Configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
MessageBox.Show("Value of other config before update: " & Convert.ToString(anotherConfig.AppSettings.Settings("MySampleAnother")))
If IsNothing(anotherConfig.AppSettings.Settings("MySampleAnother")) Then
anotherConfig.AppSettings.Settings.Add("MySampleAnother", "FreshlyAddedValueToAnotherConfig")
anotherConfig.Save(ConfigurationSaveMode.Modified)
Else
anotherConfig.AppSettings.Settings("MySampleAnother").Value = TextBox1.Text.Trim
anotherConfig.Save(ConfigurationSaveMode.Modified)
End If
MessageBox.Show("Value of other config after update: " & Convert.ToString(anotherConfig.AppSettings.Settings("MySampleAnother").value))
'To update the config files which doesnot belong to the application - End