C# – How to use ConfigurationManager

If you want to use App.config for storing some setting. Follow
use the next code for getting a parameter from a config file.

Step.1 add to “using system.Configuration”

First you Right click mouse button in Reference of Solution Explorer.
And Choice to Add Reference.

Then, Check “System.Configuration”

Add Reference

Step.2 Add to parameter in App.config

App.coinfig

Add to parameter from App.config file What you want to code in <appSettings>

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <appSettings>
    <add key ="username" value ="Sample" />
    <add key ="password" value ="Sample" />
  </appSettings>
</configuration>

Step.3 Add to code for load parameter

You can use the next code for getting a parameter from App.config file.

string name = String.Format(ConfigurationManager.AppSettings["username"]);
string pass = String.Format(ConfigurationManager.AppSettings["password"]);

class with code(full view)

using System;
using System.Collections.Generic;
using System.Configuration;

namespace configration
{
    class Config
    {
        private string GetValue(string paramName)
        {
            return String.Format(ConfigurationManager.AppSettings[paramName]);
        }
    }
}
Facebook Comments

Leave A Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.