Using app.config file in C# Visual Studio

If  would like to read some value from app.config file follow these steps.

1,Add System Configuration namespace to reference



















Otherwise you will get 'ConfigurationManager' does not exist error

2,Import name space

using System.Configuration

3,Read String from config file

ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;



4,Sample App.config file


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>
    <add name="SqlConnectionString" 
connectionString="Data Source=localhost\SQLEXPRESS;Initial 
Catalog=library;Integrated Security=True;" />

  </connectionStrings>
</configuration>






No comments:

^ Top