I've never thought about it before; but I recently learned how I could modify the app.config file to add/remove trace listeners (for example, to redirect all of the Trace.WriteLine output to a text file).
But I don't quite understand how it works? Can someone explain a bit?
I know the corresponding C# code to do the same as the config (in this example) - does that code get generated/executed before my application's entry point?
By adding an application configuration file (app. config file) to a C# project, you can customize how the common language runtime locates and loads assembly files. For more information about application configuration files, see How the runtime locates assemblies (. NET Framework).
Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.
App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
There is always one app. config file in a window application.
does that code get generated/executed before my application's entry point?
Think of a config file just as a standard text file. If your application code doesn't read and do anything with it, nothing will happen. So basically when you define some section in the app.config file, there is some code in your application (either in the BCL or custom) that at some moment will read, parse and interpret the values.
So, let's consider the example of trace listeners. When you try to trace something in your code, the underlying Trace class will use the config system to check the values you have defined in app.config. This config system parses the XML only once and stores it as singleton in memory to avoid the overhead everytime. So, it's only the first time you try to trace something that the config file is parsed and on subsequent calls the values are directly read from memory.
The app.config filed is looked by your application every time you launch it.
You can store any of your settings in your app.config files, add or remove dynamically.
Here you go...
<appSettings>
<add key="HospitalName" value="HML Hospital" />
<add key="HospitalAddress" value="Madurai" />
<add key="ServerName" value="SMSERVER" />
<add key="DatabaseName" value="HospiCare" />
<add key="DBUserID" value="sa" />
<add key="Theme" value ="Blue"/>
</appSettings>
Then you can alter the same as follows
using the namespace
using System.Configuration;
Read your config file as
string theme=ConfigurationManager.AppSettings("Theme");
and modify by using this
Configuration configFile = ConfigurationManager.OpenExeConfiguration(System.IO.Path.GetFileName(Application.ExecutablePath));
configFile.AppSettings.Settings(KeyName).Value = KeyValue;
configFile.Save();
Accept and vote up if you find your anser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With