Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When/How Does My .NET Application Use Its App.Config File?

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?

like image 510
Rob P. Avatar asked Dec 31 '11 10:12

Rob P.


People also ask

What is the use of app config file in C#?

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).

Does .NET Core use app config?

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.

What is the purpose of app config?

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.

How many app config file in ASP.NET application?

There is always one app. config file in a window application.


2 Answers

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.

like image 174
Darin Dimitrov Avatar answered Oct 03 '22 13:10

Darin Dimitrov


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

like image 44
Kishore Kumar Avatar answered Oct 03 '22 15:10

Kishore Kumar