0

I have the following code to add a new key value pair to app.config file appSettings section.

    Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.AppSettings.Settings.Add("Hello", "World");
    string name = config.AppSettings.Settings["Hello"].Value;
    config.Save(ConfigurationSaveMode.Modified);
   System.Configuration.ConfigurationManager.RefreshSection("appSettings");

    System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\xxx\xml.txt");
    file.WriteLine(name);
    file.Close();

When I run the application and open the app.config file, nothing changed. However, the string World is written on the xml.txt which means this key value pair did exist.

Tim He
  • 11
  • 3
  • Are you literally looking at the `app.config` file, or the file that's *generated* from `app.config`, e.g. `HelloWorld.exe.config`? – Jon Skeet Jul 14 '17 at 06:16
  • app.config file. I also try search "HelloWorld" in c drive but nothing shows up. @JonSkeet – Tim He Jul 14 '17 at 06:18
  • 1
    Possible duplicate of [Write values in app.config file](https://stackoverflow.com/questions/4758598/write-values-in-app-config-file) – Shakir Ahamed Jul 14 '17 at 06:28
  • You're looking in the wrong file then. `app.config` is effectively like a "source" file - on build, it's copied to `Foo.exe.config` alongside `Foo.exe`. When a configuration is saved, it's saved to `Foo.exe.config` - because that's all that would be available on a user's machine. – Jon Skeet Jul 14 '17 at 06:34
  • You are right @ShakirAhamed – Tim He Jul 14 '17 at 06:44

0 Answers0