c# - ConfigurationManager return null instead of string values -


i trying retrieve values app.config file stored in working directory, when run program returns null. confused why so, , have looked on code many times in attempt spot error.

here app.config file code:

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <appsettings>     <add key="provider" value="system.data.sqlclient" />   </appsettings>   <connectionstrings>     <add name="connection" connectionstring="data source=(local)\sqlexpress;initial catalog=autos;integrated security=true;pooling=false" />   </connectionstrings> </configuration> 

here c# code:

using system; using system.collections.generic; using system.linq; using system.text; using system.configuration; using system.data; using system.data.common;  namespace dataproviderfun {   class program   {     static void main(string[] args)     {       string p = configurationmanager.appsettings["provider"];       string c = configurationmanager.connectionstrings["connection"].connectionstring;        ... 

when run code, p = null , c = null.

i have referenced system.configuration.dll.

did ensure config file placed correctly @ directory you're running application? there file called <app name>.exe.config in directory?

i'm guessing here - maybe added app.config file in different project exe assembly project...?

by way, copied code , app.config clean project, , code worked me. i'd in direction of config file , not in code. code fine...

hope helps,

ran


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -