Social Icons

Thursday, 28 February 2013

How to Get and Write Key Values To and From App.Config File in C#

How to Get and Write Key Values in App.Config Settings file in C#


In this article i would love explain how to write key values in settings file called App.Config in C#. And also How to access the Key Values in App.Config file.


Writing key values in App.Config file

App.Config file is settings file that we will use in windows application in C#. Here we can store settings required globally for our application. Not only settings we can also write Resources, styles and global information regarding client profile info. We can write all these information in format of keys and values.

  We can write these keys in App.Config file inside appsettings tag like this

    <add key="OConStr" value="User Id=Scott;Password=tiger;Data Source=Server"/>
    <add key="SConStr" value="User Id=Sa;Password=123;DataBase=CS9DB;Data Source=Server"/>
    <add key="Cname" value="Sekhar Seo Servicess"/>
    <add key="Address" value="Sr nagar, Hyderabad - 38"/>
    <add key="Phone" value="23746666, 23745555"/>
    <add key="Email" value=""/>
    <add key="Website'" value="" />

Using <add  key="" value="'/> format these keys in app.config file

How to Access Key Values from App.Config file:

To Access key values from app.config file import namespace System.Configuration.

Now this globally available information can be accessed in code behind files using Get method as follows

string cname = ConfigurationManager.AppSettings.Get("Cname");

Here Cname is key value settings file.

            string cname = ConfigurationManager.AppSettings.Get("Cname");
            string addr = ConfigurationManager.AppSettings.Get("Address");
            string phone = ConfigurationManager.AppSettings.Get("Phone");
            string email = ConfigurationManager.AppSettings.Get("Email");
            string Constr = ConfigurationManager.AppSettings.Get("SConStr");

Now we can use this values wherever required.


  To Communicate with settings files we have to import Namespace System.Configuration. Then we can write settings in key value pairs. Then we can access that key values using ConfigurationManager.AppSettings.Get(//keyname) .


I would like to have feedback from my blog readers. Your valuable feedback,Suggestions,Questions, Comments about this article are always welcome.

No comments:

Post a Comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.