How to use App.config in Visual Studio C# .NET

Add reference to System.Configuration :


and include it using the using statement:

using System.Configuration;

Edit App.config so that you add your settings in the appSettings  node in an add  element with key  and value  parameters.

<?xml version="1.0" encoding="utf-8" ?>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

      <add key="server" value="localhost" />
      <add key="port" value="5432" />
      <add key="username" value="myusername" />
      <add key="password" value="mypass" />
      <add key="database" value="mydb" />

Now you can access your settings in code like this:

string server = ConfigurationManager.AppSettings["server"].ToString();
string port = ConfigurationManager.AppSettings["port"].ToString();
string username = ConfigurationManager.AppSettings["username"].ToString();
string password = ConfigurationManager.AppSettings["password"].ToString();
string database = ConfigurationManager.AppSettings["database"].ToString();


Written by Nikola Brežnjak