.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Read and Write a Application configuration using C#.net

Posted By:Narayanan       Posted Date: March 28, 2012    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

Here, I give an read and Write an Application Configuration file using C#.net.
 

First , we know the meaning of Application Configuration file.

Application Configuration file:
             It is simply says that Store configuration Information.you can store a information in the structure like this Key and Value.Each application can have a One configuration file.It is a XML file .you can open any format (like Notepad).

Now we will go to our Topic:

How to create a Application configuration file in our Application?

  Let start ,
  1.   Create a Project .
  2.   "Right click" on the Project name in the Solution Explore.
  3.   Click "Add" and Click "New Item".
  4.    Add New Item window will display.
  5.    Click "Application Configuration file" in the window.
  6.    Give a  Name of the Config file or Default name displays in the window like this  (App1.config).
  7.      Click "ADD" button in the Window.

Now, we see the lines in the Config File



Now we add some keys and Values between appSetting tag.Before we add a appSetting between configuration tag.



  
    
  
Now ,How to add a Key and Value  using C#.net?

ADD a Key and Value:

In my Project , I create a Class for ADD and Read Key and  value in the Configuration file.

Create a Class file .
My Class file Name is AppConfig.cs

Declare a Some Namespace in the Class.
using System.Configuration;
using System.Windows.Forms;

I create a small function for adding a Key and Value in the Configuration file.

     public static void AddintoConfigsetting(string appkeys,string appvalues)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add(appkeys, appvalues);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
            ShowConfigsetting();

        }

To retrieve the keys and Values from Configuration file

     public static void ShowConfigsetting()
        {
            foreach (string key in ConfigurationManager.AppSettings)
            {
                string value = ConfigurationManager.AppSettings[key];
                string result = "Keys :" + " " + key + " " + "Key value:" + " " + value;
                MessageBox.Show(result);
            }
        }

  Now we go to our form:
  1.    Create a form
  2.    Form contains Two Command buttons.
  3.    One is "Add Key and Value"
  4.    another one is "Read Keys and Values"

 call the class file function in the Form.cs

add the following code click event of  ADD Key and Value button

AppConfig.AddintoConfigsetting("EmpId","1010");
add the following code click event of  Read Keys and Values button
   AppConfig.ShowConfigsetting();

Conclusion:

    Today we learn the Application Configuration file.Thanks for spend a value time .


 
        
     
 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend