0

I have a new (test) console application that I'm trying to access the settings.

enter image description here

When I attempt to access it this way, from what I've read on SO and other places, this should work:

 var test1 = System.Configuration.ConfigurationManager.AppSettings["MySetting"];

but it doesn't, it returns null.

When I do it this way, it works fine:

var test2 = Properties.Settings.Default.MySetting;

Why doesn't the first one work? Everything I've read shows to use it the first way.

Am I using it incorrectly?

EDIT

In response to Mason.

I'm confused on your comment. The app.config is where the setting is being set from the properties of the application:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ConsoleApp1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    </startup>
    <applicationSettings>
        <ConsoleApp1.Properties.Settings>
            <setting name="MySetting" serializeAs="String">
                <value>testtesttest</value>
            </setting>
        </ConsoleApp1.Properties.Settings>
    </applicationSettings>
</configuration>
ErocM
  • 4,505
  • 24
  • 94
  • 161
  • I believe ConfigurationManager gives you acess to app.config but you want to get application properties. Where did you read that the first code line was correct? – derloopkat Nov 02 '18 at 17:58
  • @derloopkat Here is just one example of many I found. Maybe I'm missing something. https://stackoverflow.com/questions/2400097/reading-from-app-config-file – ErocM Nov 02 '18 at 17:59
  • 1
    The `` is not the same as putting it into the app settings file – Joe Phillips Nov 02 '18 at 18:03
  • see *"Where are the Properties.Settings.Default stored? "* https://stackoverflow.com/questions/982354/where-are-the-properties-settings-default-stored – derloopkat Nov 02 '18 at 18:05

2 Answers2

2

System.Configuration.ConfigurationManager.AppSettings is for reading settings from the app.config or web.config files (for ASP.NET). It does not read from the settings files.

mason
  • 31,774
  • 10
  • 77
  • 121
0

Because AppSettings refers to reading the settings in MyApp.config

Austin T French
  • 5,022
  • 1
  • 22
  • 40