Walter Fabio Simoni
Walter Fabio Simoni

Reputation: 5729

Why no AppSettings in my ConfigurationManager?

I don't know why but i don't have AppSettings in my app.config.file. If i add it, when i run my apps, appSetting is deleted, so i'm unable to read my data !

Here is my file :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</configuration>

And in c# :

string RepertoireEntree = ConfigurationManager.AppSettings["RepertoireEntree"];

Upvotes: 0

Views: 951

Answers (2)

granadaCoder
granadaCoder

Reputation: 27904

You do not have your xml correct in the app.config file.

note the start and end tags for appSettings

<?xml version="1.0" encoding="utf-8" ?>
<configuration>



<appSettings>
    <add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
    <add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>


</configuration>

PS

While this is probably not an issue for you (but moreso for future readers)

Make sure you have this reference:

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

as noted on MSDN

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157136

You have your app settings in a appSettings element, else it won't be recognized:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
        <add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
    </appSettings>
</configuration>

By the way, it is easier to use settings the usual way. You can do this by creating a settings file from your Project Settings > Settings tab.

Visual Studio will generate the XML elements in the app.config for you and you can reference your property like this:

string re = Properties.Settings.Default.RepertoireEntree;

Upvotes: 2

Related Questions