App.config file - DotNet and DataBase

Wednesday, 1 August 2018

App.config file



Under the .Netframework application folder, we can find a file with name "App.config" which is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information. Developers can maintain settings like the data settings and connections which are common for all users and no need to change by the user. If the developer wants to change the settings in the App.config page, it is not required to build/rebuild the application and deploy.

Configuration files are XML files that can be changed as needed. An administrator can control which protected resources an application can access, which versions of assemblies an application will use, and where remote applications and objects are located.

Configuration File Format:
Configuration files contain elements, which are logical data structures that set configuration information. Within a configuration file, we use tags to mark the beginning and end of an element. For example, the <appSettings> element consists of <appSettings>child elements</appSettings>. An empty element would be written as <appSettings/> or <appSettings></appSettings>. App.config, XML file tags are case sensitive.



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <startup>
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      </startup>
   <appSettings>
      <add key="AdminName" value="Naga"/>
      <add key="AdminEMail" value="Naga@gmail.com"/>
    </appSettings>
    <connectionStrings>
      <add name="connection" connectionString="Data Source=LAPTOP-xyzmno;Initial Catalog=EmployeeData;User ID=sa; Password=123456"/>
    </connectionStrings>
</configuration>


As per above snippet, the tags under <appSettings></appSettingsare global variables we can access them in the application as below



string emailid = System.Configuration.ConfigurationManager.AppSettings["AdminEMail"];

string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

Using AppSettings methods we can read the variables that are configured under <appSettings/tag and using <connectionStringswe can access the connection string across the application.


No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.