Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# How to Get key/value pairs from app.config and web.config

C# How to Get key/value pairs from app.config and web.config

This article shows you the way how to get key/value pairs from app.config used in window application and web.config used in web application.

1. How to Get key/value pairs from app.config

Use property AppSettings of class ConfigurationSettings in namespace System.Configuration

using System.Configuration;
...
string conString = ConfigurationSettings.AppSettings["MyDBConnection"];

using System.Configuration; ... string conString = ConfigurationSettings.AppSettings["MyDBConnection"];

Below is a sample of App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="MyConnection" value="Server=(local)\SQL2K8EXPRESS;uid=sa;pwd=;database=MyDB"/;
  </appSettings>
</configuration>

<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="MyConnection" value="Server=(local)\SQL2K8EXPRESS;uid=sa;pwd=;database=MyDB"/; </appSettings> </configuration>

2. How to Get key/value pairs from web.config

Use property AppSettings of class ConfigurationManager in namespace System.Configuration

using System.Configuration;
...
string ourBlog = ConfigurationManager.AppSettings["OurBlog"].ToString();

using System.Configuration; ... string ourBlog = ConfigurationManager.AppSettings["OurBlog"].ToString();

Below is a sample of web.config

<configuration>
    <configSections>
    </configSections>
 
    <appSettings>
      <add key="OurBlog" value="http://4rapiddev.com"/>
    </appSettings>
 
    <connectionStrings>
      <add name="MyDBConnection" connectionString="Server=(local)\SQL2K8EXPRESS;uid=sa;pwd=;database=MyDBConnection"/>
    </connectionStrings>
    <system.web>
 
      <httpRuntime executionTimeout="240" maxRequestLength="20480" />
    </system.web>
</configuration>

<configuration> <configSections> </configSections> <appSettings> <add key="OurBlog" value="http://4rapiddev.com"/> </appSettings> <connectionStrings> <add name="MyDBConnection" connectionString="Server=(local)\SQL2K8EXPRESS;uid=sa;pwd=;database=MyDBConnection"/> </connectionStrings> <system.web> <httpRuntime executionTimeout="240" maxRequestLength="20480" /> </system.web> </configuration>

Mar 5, 2012quynhnguyen
PHP Copy Entire Directory To Another DirectorySQL Server Get list of dates between start date and end date
You Might Also Like:
  • Get Or Read AppSettings Item Value From The Web.Config File
  • Add Mime Type (.FLV, MP4) To Web.Config In IIS 7
  • Asp.net Load Connection String Dynamically For Different Environments
  • Implement Google Captcha (reCAPTCHA) With ASP.NET
  • C# Get List of databases in SQL Server
  • Differences Between .NET Framework 4.0 And Earlier Releases
  • How To Track Website With Multiple Google Analytisc Accounts
  • ASP.NET How to configure Ajax Control Tookit in Visual Studio
  • Collabtive: Class DateTimeZone could not be located
  • Auto Convert Text To URL Link And MailTo Email Address With ASP.NET C#
quynhnguyen
9 years ago CSharpAppSettings, ASP.NET, Config, ConfigurationManager, CSharp489
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,182 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,058 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,839 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,313 views
C# Read Json From URL And Parse/Deserialize Json
9,809 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
  • How to Choose the Most Effective Free Photo Editor on the Web
  • How to Compose Your Essay To Me – 4 Easy Steps to Write My Essay
  • How to Find a Photo Editor
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (109)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development