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"]; |
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> |
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(); |
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> |