Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading settings from app.config in .net 4.5

Tags:

c#

app-config

I've added a reference to System.Configuration. I've created App1.config in my project and populated it with the following code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="ResistanceA" value="0.04"/>
  <add key="ResistanceB" value="0.04"/>
  <add key="ResistanceC" value="0.01"/>
  <add key="TempBattLow" value="40"/>
  <add key="TempBattHigh" value="45"/>
  <add key="TempLoad" value="40"/>    
 </appSettings>
</configuration>

Then I try to read the values using the following code,

using System.Configuration;

string str = ConfigurationManager.AppSettings.Get("ResistanceA");

However I do not get the data. Any idea what I am doing wrong? Thanks.

like image 883
jules0075 Avatar asked Oct 19 '25 16:10

jules0075


1 Answers

Make sure the (app name here).config file actually appears in the same folder as your (app name here).exe file. Being that you called it App1.config, I'm guessing that you have more than one.

Visual Studio renames App.Config to the actual (app name here).config file during a build, not App1.config.

like image 123
Nathan A Avatar answered Oct 22 '25 06:10

Nathan A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!