WPF application , modify the App.Config file from compiled application

Kapanther 21 Reputation points

I have probably misunderstood the documentation because i'm a hack :p. But is the App.Config a read-only situation in a WPF application?

System.ConfigurationManager.AppSettings seems to have a "Set" and "Get" method . But for the life of me I can not get the "Set" method to modify this file in a compiled application. I have a DatabaseName field in my App.Config etc.. and i'm pretty sure i'm calling it correctly.

App.Config File

<?xml version="1.0" encoding="utf-8" ?>
<add key="DatabaseName" value=""/>
<add key="DatabaseUsername" value=""/>
<add key="DatabasePassword" value=""/>

Calling Code

ConfigurationManager.AppSettings.Set("DatabaseName", someDatabaseNameString);

I'm guessing this is by-design ( a sort of safe-place for application-wide settings) . If this is the case I guess i can just store "application-wide" parameters that need to be changed by the application in a different config file.

But would be good to know if this is the case. Would much rather have one config file then two.

(P.S) yes im running the compiled application and checking to see if the <ApplicationName>.dll.config is been changed


Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,689 questions
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
772 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,311 Reputation points Microsoft Vendor

    @Kapanther , Welcome to Microsoft Q&A, based on my test, I reproduced your problem.
    I recommend that you use the following code to change your app.config value.

    private void btntest_Click(object sender, RoutedEventArgs e)  
                AddUpdateAppSettings("DatabaseName", "connstr");  
            AddUpdateAppSettings("DatabaseUsername", "testuser");  
            AddUpdateAppSettings("DatabasePassword", "password");  
            public void AddUpdateAppSettings(string key, string value)  
                    System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();  
                    fileMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config";  
                    Configuration configFile = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);  
                    var settings = configFile.AppSettings.Settings;  
                    if (settings[key] == null)  
                        settings.Add(key, value);  
                        settings[key].Value = value;  
                catch (ConfigurationErrorsException)  
                    Console.WriteLine("Error writing app settings");  

    Best Regards,

    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful