ErocM
ErocM

Reputation: 4662

Accessing Settings in different ways

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>

Upvotes: 0

Views: 238

Answers (2)

mason
mason

Reputation: 32728

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.

Upvotes: 2

Austin T French
Austin T French

Reputation: 5140

Because AppSettings refers to reading the settings in MyApp.config

Upvotes: 0

Related Questions