45

We know that IIS caches ConfigurationManager.AppSettings so it reads the disk only once until the web.config is changed. This is done for performance purposes.

Someone at:

http://forums.asp.net/p/1080926/1598469.aspx#1598469

stated that .NET Framework doesn't do the same for app.config, but it reads from the disk for every request. But I find it hard to believe, 'cause it would be slower. Please tell me that he is wrong or I'll have to fix every Console/Windows Forms/Windows Services I wrote.

Update I regret that I misinterpreted what people said in the linked forum above.

dove
  • 20,469
  • 14
  • 82
  • 108
Jader Dias
  • 88,211
  • 155
  • 421
  • 625

6 Answers6

50

A quick test seems to show that these settings are only loaded at application startup.

//edit the config file now.
Console.ReadLine();

Console.WriteLine(ConfigurationManager.AppSettings["ApplicationName"].ToString());
Console.WriteLine("Press enter to redisplay");

//edit the config file again now.
Console.ReadLine();
Console.WriteLine(ConfigurationManager.AppSettings["ApplicationName"].ToString());
Console.ReadLine();

You'll see that all outputs remain the same.

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • 22
    Just to be picky, settings get loaded the first time they're referenced not necessarily at application startup. – Samuel Neff Jan 22 '10 at 22:49
  • 10
    To be even more picky (and off topic to boot) - no need to call ToString() - it's already a string – Oskar Austegard Mar 09 '12 at 23:01
  • 1
    You just needed to check the System.Configuration.ConfigurationManager class and you'll see that the class and its properties are static – heymega Feb 25 '14 at 10:42
  • 6
    You will have to call `ConfigurationManager.RefreshSection("appSettings")` in order to get the changes. You could also add a file watcher to reload it only when it changes. – Luis Perez Jul 21 '15 at 18:35
  • Why would one access in anything but a static constructor, then? (assuming a watch isn't added) Also -- @SamuelNeff Are all settings read on first reference, or are you saying different settings are read from the same file at different times? If it's the latter, I'm starting to wonder about our friends in Redmond. – ruffin Sep 11 '20 at 21:16
4

Try it,

ConfigurationManager.RefreshSection("appSettings")

Just be careful file name (in bin folder)

Normal file name : appname.exe.config

if debug mode : appname.vshost.exe.Config

Ali Osman Yavuz
  • 389
  • 3
  • 4
3

It doesn't matter if it does or not. Don't fix a performance problem if there isn't one.

John Sonmez
  • 7,128
  • 5
  • 37
  • 56
  • 53
    I somewhat disagree here. Configuration values like this are likely to be used in all kinds of places in your application, including nested loops and such. Grokking whether the read of a config file will take microseconds vs. milliseconds is important to know. – Dave Markle Dec 24 '08 at 18:55
3

It reads the application configuration file (MyApp.exe.config) once at application startup, as can easily be verified by changing the file while the app is running.

The comment in the forum post referenced by the OP was:

The values for the Web.config are stored into cache/memory when the application starts hence why the app restarts when any changes are made to the web.config. Note that this only applies to the Web.config, any other .config files you may use are accessed from the disk by default

I would interpret this comment as meaning that config files other than web.config in an ASP.NET application are accessed from the disk by default. And similarly, config files other than MyApp.exe.config in a WinForms/Console application are accessed from the disk by default.

This comment is not stating that MyApp.exe.config is read from the disk by default.

Joe
  • 122,218
  • 32
  • 205
  • 338
2

AppSettings is cached. You can improve performance by further caching to limit namevaluecollection lookups.

See: DotNetPearls Static Config Pattern

HAL9000
  • 1,002
  • 2
  • 17
  • 28
1

As John says only spend more time on this if you are actually seeing a performance hit.

Also I'm pretty sure that these applications hold the configuration in memory, and to see any changes within a config the application would have to be restarted.

For some further reading about remaining mysteries.

dove
  • 20,469
  • 14
  • 82
  • 108