Add key and value to App.config during runtime

This blog article shows you how to add value to App.config during runtime and get the value back. This works for sure for .NET Framework. I am still finding how to do this for .NET Core. In fact, I just got some disappointing fact that this might not happen and people ask you to go for json file. So again, you are Framework or you are core. Write once run everywhere is hard to happen. Nevertheless, let’s look at App.config that existed and good to use for many years.

I have partial App.config file like the one below.

<appSettings>

<add key=1value=fpx />

<add key=6value=creditcard/6 />

</appSettings>

I have a GetAppSetting method to get the AppSettings value.

public static void GetAppSetting(string value)

{

    string str = System.Configuration.ConfigurationManager.AppSettings[value];

    Console.WriteLine(str);

}

I have a AddKey method to add the new value to App.config during run time. It is actually added to ConAppSetting.exe.Config in the bin folder. ConAppSetting. is my project name.

public static void AddKey()

{

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

    config.AppSettings.Settings.Add(“6”, “creditcard/6”);

    config.Save(ConfigurationSaveMode.Modified,true);

    ConfigurationManager.RefreshSection(“appSettings”);

}

To test the code, you can use the main function like the one below.

static void Main(string[] args)

{

    GetAppSetting(“1”);

    AddKey();

    GetAppSetting(“6”);

    Console.WriteLine(“Press any key to continue…”);

    Console.ReadKey();

}

You will see new entry added to the file.

<appSettings>

<add key=1value=fpx />

<add key=6value=creditcard/6 />

</appSettings>


Source code: https://github.com/chanmmn/AppConfig

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.openexeconfiguration?view=dotnet-plat-ext-3.1/?WT.mc_id=DP-MVP-36769

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet and tagged . Bookmark the permalink.

1 Response to Add key and value to App.config during runtime

  1. Pingback: exePath must be specified when not running inside a stand alone exe | Chanmingman's Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.