Edit App.Config at runtime : C#


Config file will look like – 

<appSettings>

<add key=”Applications” value=”Proj1|Proj2|App1|App2|App3″/>

</appSettings>

C# code will like this – 

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection configSection = config.AppSettings;

try
{
if (configSection != null)
{
if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false)
{
configSection.Settings["KEY"].Value += “|NewValue”;
ConfigurationManager.RefreshSection(“appSettings”);
config.Save(ConfigurationSaveMode.Modified);
}
}
}
catch (ConfigurationException ex)
{
MessageBox.Show(ex.Message, “Configuration Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

About these ads

4 thoughts on “Edit App.Config at runtime : C#

    • MrNoOne, We are using this code in multiple projects and works fine, so if you can share snapshot of your code, I will try to look at it … It looks like there will be very minor things. You didn’t mentioned how your config file look like and code… It’s little rough to just say ‘DOES NOT WORK…’ & In capital letters…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s