0

I have created a simple windows application and added a App.config file

I have also added Settings file which also adds keys into App.config file.

<appSettings> 
    <add key="myKey" value="1"/>    
</appSettings>
<applicationSettings>
    <MyApp.AppSettings>
        <setting name="RunAutomated" serializeAs="String">
            <value>False</value>
        </setting>
    </MyApp.AppSettings>
</applicationSettings>

Inside my code have the following :

If(AppSettings.Default.RunAutomated) {
    Console.WriteLine("Running auto!!!");
}
else { 
    Console.WriteLine("Running manual!!!");
}

Simple is that.

When I build the project and run it through visual studio everything is as expected that output changes when I change RunAutomated key and re run the application.

But this is not true when I go under /bin/Debug folder and run the MyApp.exe

It seems to cache app.config and it ignores the changes to app.config file.

How should I do this simple task to keep reading the App.config file every time it is run? any changes I am making app.config should affect the application?

4
  • I'm sure there's a duplicate somewhere, but your app.config is copied to AppName.exe.config every build. So when rebuilding, you're back to defaults. Commented Jan 15, 2015 at 13:12
  • 1
    I'd only use app.config where absolutely necessary (e.g. assembly redirection). For custom configuration I'd use a separate file together with some serializer. Commented Jan 15, 2015 at 13:16
  • Can you explain better how do you change that value? Do you change it in code? Do you change it in app.config before running the debug session? Directly on the exe.config in the debug folder? Commented Jan 15, 2015 at 13:27
  • Changing AppName.exe.config seems to be working instead of changing app.config file. Thanks Commented Jan 15, 2015 at 15:17

1 Answer 1

1

If you have altered the property in code, or using a UI in your app, you should save the altered properties by calling Save on the SettingsBase instance:

AppSettings.Default.Save();

Note that after every build the settings file is overwritten, so try to copy the executable to another folder and run it multiple times.

Alternatively you can create your own settings file, which I find easier and more reliable. You can take a look at the XmlSerializer class and the JsonConvert class in Newtonsoft Json.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.