Tom
Tom

Reputation: 16246

Writing key value pairs into app.config file

It works fine with Read, but I can't seem to get the Write happening. It doesn't throw any IO Exception when I try to write file. As if nothing happened.

Here is my code, please look at the GetValue() and SetValue() functions:

using System.Configuration;

public class AppConfig {
    private string _username;
    private string _password; 

    public AppConfig() {
        _filePath = GetValue("Username");
        _password = GetValue("Password");
        //... more
    }
    public string Password {
        get { return _password; }
        set { SetValue("Password", value); _password = value; }
    }
    public string Username {
        get { return _username; }
        set { SetValue("Username", value); _username = value; }
    }
    private void SetValue(string key, string val) {
        var cfg= ConfigurationManager
                .OpenExeConfiguration(ConfigurationUserLevel.None);
        cfg.AppSettings.Settings[key].Value = val;
        cfg.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
    private string GetValue(string key) {
        return ConfigurationManager.AppSettings[key];
    }
}

And this is the app.config file:

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

    <add key ="Password" value ="123456"/>
    <add key ="Username" value ="hohoho"/>

  </appSettings>
</configuration>

Any idea how to make the Write happening? Thank you.

Upvotes: 1

Views: 7800

Answers (2)

Anish
Anish

Reputation: 51

Try the following code

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        config.AppSettings.Settings[key].Value = value;
        config.Save();
        ConfigurationManager.RefreshSection("appSettings");

Upvotes: 0

Andre Luus
Andre Luus

Reputation: 3802

The configuration system in .Net was not designed to let you programmatically save changes to the app.config in the assembly folder. I know there is a "Save" method there, but those changes actually save to a copy of the config file. The location of the copy depends on the scope of the setting.

Application Settings such as these:

<appSettings>
     <add key="" value=""/>
</appSettings>

Have "application scope" if I recall correctly. If you use the Settings page in the project settings page or open Settings.settings in the Properties folder, you can choose the scope (User or Application).

This way, changes to the settings are persisted for the current user or for that specific version of the application. These copies are stored in a generated folder somewhere in the %APPDATA% for the appropriate account. The configuration system automatically loads the settings again depending on who's logged in.

This is why you also have an "Upgrade" method. It allows you to add settings to new versions of your application and upgrade the user's settings, causing only the new properties to be added to the user's copy. In this way, the setting in the app.config in the assembly folder is only the default value for the setting.

The comments above touch on why this is the way it works: Files inside Program Files are intended to remain untouched after installation and UAC ensures this.

I suggest reading up on the configuration system, it is quite powerful. If you still really want to modify app.config, you have to write custom code to modify the app.config file directly and

  • install into user's profile folders, or
  • always run your program as an administrator

Upvotes: 2

Related Questions