Reading value from App.Config C# Windows Application - Winforms

We may require small or medium changes in our code to run it in different machines and differnt configurations, Recompailing and Regenerating .exe files / dlls are a not a good practice. So we are using configuration files or settings files. Web.config is an example of such type file in web application. In Winforms or Windows application we need to use App.Config to achive the goal.

Here is an example to store and retreive the value from App.Config

Initially add an App.Config file to your project then set the values like following within the < appSettings > < /appSettings > block


< appSettings >
< add key="Name" value="Mathew Philip"/> < /appSettings >


In the code behind you should import the following namespace



using System.Configuration;



Then retreive the value from the App.Config by the following code


string name = Convert.ToString(ConfigurationSettings.
AppSettings["Name"])


So that you can simply edit the XML in the App.Config and change your values without reconstructing the Exe file or Dll file. You can encript and store the contents if you want more security.


Comments

Author: Atul Vishwakarma23 Jun 2009 Member Level: Bronze   Points : 1

Goog yaar,
but please provide complete example using C# and SQL with app.config.

Author: Vijayaragavan.R23 Jun 2009 Member Level: Bronze   Points : 0

This is nice one,

Please give me an example of read an value from it.

Guest Author: jeremy08 Apr 2013

AppSettingsReader reader = new AppSettingsReader();
string value = reader.GetValue("BizTalk Server Administration", typeof(string));



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: