Angular, PWA, C#, ASP.NET, Node.js, Docker, React, IONIC, IoT & more …

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);
}

4 responses to “Edit App.Config at runtime : C#”

  1. MrNoOne Avatar
    MrNoOne

    DOES NOT WORK!!!!

    Like

    1. Bhavin Patel Avatar
      Bhavin Patel

      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…

      Like

  2. Ranendra Avatar
    Ranendra

    got error..
    Attempted to perform an unauthorized operation.

    Like

  3. Haddy58 Avatar
    Haddy58

    localhost:54574/

    Like

Leave a reply to Haddy58 Cancel reply