23 August 2012

Read and Write a Application configuration using C#.net

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 ,
      Create a Project .
      "Right click" on the Project name in the Solution Explore.
      Click "Add" and Click "New Item".
       Add New Item window will display.
       Click "Application Configuration file" in the window.
       Give a  Name of the Config file or Default name displays in the window like this  (App1.config).
         Click "ADD" button in the Window.
Now, we see the lines in the Config File

    <!--?xml version="1.0" encoding="utf-8" ?-->
    <configuration>
    </configuration>

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

    <!--?xml version="1.0" encoding="utf-8" ?-->
    <configuration>
  <appsettings>
        <add key="C#" value="EasyToLearn"></add>
      </appsettings>
    </configuration>

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

    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:
       Create a form
       Form contains Two Command buttons.
       One is "Add Key and Value"
       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 .

No comments: