Web.config Vs App.config Vs Machine.config

I will let you know why we use it. As you know all are configuration files which are used to store the data which would be key –value.

As per security purpose this data is known as secure data because it cannot be hacked by some external resource directly. So, let's start one by one to know more about these configuration files.

Web.config

It is a configuration file, which is used in web application and it can be an ASP.NET project or MVC project. Some project contains multiple web.config file inside the same project but with different folder. They have their unique benefits. You can create several web.config file in each folder with their unique benefits as per your project requirement.

It is used to store the application level configuration. Sometimes it inherits the setting from the machine.config. It parses at runtime, means if you make any changes then web application will load all the settings in the config file. You don’t need to create a web.config file because it is auto generated when you create a new web application. If you want to create a web.config manually you can create it.

Sample Example

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.web>  
  4.   
  5.   </system.web>  
  6. </configuration>  
Web.config

App.config

It is also a special type of configuration file which is basically used with Windows Services, Windows application, Console Apps or it can be WPF application or any others.

It parses at compile time; it means if you edit the app.config when program is running, then you need to restart the application to reload the configuration setting into the program.

When you run the application which contains the app.config, at the time of compilation a copy of app.config with different name moves into build folder for running the application, So that's why we need to restart the program if any changes made in app.config.

It is not added auto when you create a project, to add this you need to go to solution explorer and choose Add New Item and choose “Application Configuration File”. Windows application always contains the App.config file into the project.

Sample Example
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.     <connectionStrings>     
  4.         <add name="MyKey"   
  5.              connectionString="Data Source=localhost;Initial Catalog=ABC;"  
  6.              providerName="System.Data.SqlClient"/>  
  7.     </connectionStrings>  
  8. </configuration>  
App.config

Machine.config

It is a special type of configuration file which creates into the OS when you install visual studio. This stores machine level configuration setting. Only one machine.config file exists into the system and it stores highest level of configuration settings.

Machine.config settings apply to all web applications which is residing on the server. The setting of machine.config can be overridden by web.config’s settings. If your system does not contain the machine.config then you cannot execute the application.

Path of Machine.config

 

    32-bit System

    %windir%\Microsoft.NET\Framework\[version]\config\machine.config

    64-bit System

    %windir%\Microsoft.NET\Framework64\[version]\config\machine.config

Sample Example

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!--  
  3.     Please refer to machine.config.comments for a description and  
  4.     the default values of each configuration section.  
  5.   
  6.     For a full documentation of the schema please refer to  
  7.     http://go.microsoft.com/fwlink/?LinkId=42127  
  8.   
  9.     To improve performance, machine.config should contain only those  
  10.     settings that differ from their defaults.  
  11. -->  
  12. <configuration>  
  13.   <configSections>  
  14.   </configSections>  
  15. </configuration>  
Machine.config

Thanks for reading the article, Hope you enjoyed.