Question

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.

Was it helpful?

Solution

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

This is efficient and baked in Visual Studio.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top