Home > Code > C# > How to access App.config file setting using C#

How to access App.config file setting using C#

by Narain Siddharth   on May 08, 2011   Category: C#   |  Views: 1470    |  Points: 25   |  Silver 


See the below sample App.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connection" value="Data Source=Server_Name;Initial Catalog= Database_Name;UserId=Username;Password=Password"/>
</appSettings>
</configuration>


Code snippet for accessing connection string from App.config file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string conn = ConfigurationSettings.AppSettings["connection"];
MessageBox.Show(conn);

}
}
}




Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts