views:

112

answers:

2

I'm using VS 2008 with .NET 3.5, and I'm having trouble retrieving app settings when debugging. I've added a reference to System.Configuration and the console app compiles and runs, but when I try and retrieve an app setting, the return value is always null.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="ConsoleApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
 </configSections>
 <applicationSettings>
    <ConsoleApp.Properties.Settings>
        <setting name="LogDirectory" serializeAs="String">
            <value>\c$\TestApp\LOG\</value>
        </setting>
    </ConsoleApp.Properties.Settings>
 </applicationSettings>
</configuration>

Code snippet:

string logPath = @"\\" + machineName + ConfigurationManager.AppSettings["LogDirectory"];

Am I doing something obviously wrong here?

EDIT: to clarify, that app.config XML was auto-generated. I added a new Application Configuration File item to the project, and then used the settings tab of the project properties window to add the LogDirectory setting.

+1  A: 

You need something like this - app settings is essentially a dictionary of strings.

<configuration>
  <appSettings>
    <add key="LogDirectory" value="\c$\TestApp\LOG\"/>
  </appSettings>
</configuration>

Have a look here for more info on app settings.

Fiona Holder
+2  A: 

Why not just use Properties? You can access your properties using Properties.Settings.Default.WhatEverYouWant?

This is efficient and baked in Visual Studio.

Stéphane
Could you give a code example please? I've tried using "Properties.Settings.Default.LogDirectory" and I get a compilation error stating that "The name 'Properties' does not exist in the current context".
Talvalin
This is your settings:<applicationSettings> <ConsoleApplication2.Properties.Settings> <setting name="SettingName" serializeAs="String"> <value>This a the value</value> </setting> </ConsoleApplication2.Properties.Settings> </applicationSettings>You can acces it via Properties.Settings.Default.SettingName
Stéphane
Sorry the formatting of the XML portion didn't hold. But the bottom line is to use : Properties.Settings.Default.SettingNameAnd BTW you have to go to your project Properties and Add A setting this will add an app.config file and the you will have access to the properties.
Stéphane
I encountered a minor problem because of a difference between the namespace of my project and the actual project name. Once I fixed that, your suggestion worked fine so thank you!
Talvalin