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

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

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