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

Kapanther 21 Reputation points
2022-03-02T09:30:09.54+00:00

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" ?>
<configuration>
<appSettings>
<add key="DatabaseName" value=""/>
<add key="DatabaseUsername" value=""/>
<add key="DatabasePassword" value=""/>
</appSettings>
</configuration>

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

Thanks

Developer technologies Windows Presentation Foundation
Developer technologies XAML
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-03-03T06:23:49.013+00:00

    @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)  
            {  
                try  
                {  
                    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);  
                    }  
                    else  
                    {  
                        settings[key].Value = value;  
                    }  
                    configFile.Save(ConfigurationSaveMode.Modified);  
                    ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);  
                }  
                catch (ConfigurationErrorsException)  
                {  
                    Console.WriteLine("Error writing app settings");  
                }  
            }  
    

    Result:
    179504-7.gif
    Best Regards,
    Jack


    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.