none
App.config change file problem

    Question

  •  

    hello

    I am trying to change my app.config file. I have added "Application Configuration file" to the project. then add <appSettings> tag and a key. when I build the solution two files will be added to my application folder: "appName.exe".config and "appName.vshost.exe.config"

    I don't have any problem to read from config file with AppSettingsReader or System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    but when I try to change the appSettings's keys on config file programatically I have faced many problems. first of all, the methods which have been served by classes like ConfigurationManager are affecting to "appName.vshost.exe.config" and they affect untill I close the application. after restarting,all changes will disappear.

    If I hard change the appName.exe.config as a plain xml file then the changes will affect after restarting the application not before! and also none of the methods will affect to App.config file which is listed in my project!

    I don't know the differences between these 3 config files and what I really want is to make changes to appSettings and I want the changes be affected immediately and for all related config files. but I don't know what is the best and easiest way to do it.

    thank you in advance

    regards

     

    Friday, November 16, 2007 8:45 AM

Answers

  • Hi ashk1860,

    After making changes to the application configuration file, you’d call Configuration.Save() to save your changes. Then you can call ConfigurationManager.RefreshSection() to force a reload of the changed section.

    For detail information and sample code, you can visit:

    http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

     

    Monday, November 19, 2007 8:59 AM
  •  

    static void UpdateAppSettings()

    {

    //replace with your project name .exe file

    string configPath = System.IO.Path.Combine(Environment.CurrentDirectory, "YourProject.exe");

    System.Configuration.Configuration config =

    ConfigurationManager.OpenExeConfiguration(configPath);

    // Add an entry to appSettings.

    int appStgCnt =

    ConfigurationManager.AppSettings.Count;

    string newKey = "ConnectionString" ;

    string newValue = "ine";

    config.AppSettings.Settings.Add(newKey, newValue);

    // Save the configuration file.

    config.Save(ConfigurationSaveMode.Minimal);

    // Force a reload of the changed section.

    ConfigurationManager.RefreshSection("appSettings");

    }

    Monday, November 19, 2007 10:56 AM
  • You need to specify explicitly app config file name else it would take the default one .vshost.exe, try this code it should work

    Thanks

     

    Monday, November 19, 2007 10:59 AM
  • yes! the solution is to add the following line before adding and it won't throw exception if the key is not exist:

    config.AppSettings.Settings.Remove(KEYNAME);

     

    Monday, November 19, 2007 12:22 PM

All replies

  • Hi ashk1860,

    After making changes to the application configuration file, you’d call Configuration.Save() to save your changes. Then you can call ConfigurationManager.RefreshSection() to force a reload of the changed section.

    For detail information and sample code, you can visit:

    http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

     

    Monday, November 19, 2007 8:59 AM
  • hi

    thank you for your answer. I have the same problem. I mean it just work with "AppName.vshost.exe.config" not with the other two files. I just copy and paste update method like:

    static void UpdateAppSettings()

    {

    // Get the configuration file.

    System.Configuration.Configuration config =

    ConfigurationManager.OpenExeConfiguration(

    ConfigurationUserLevel.None );

    // Add an entry to appSettings.

    int appStgCnt =

    ConfigurationManager.AppSettings.Count;

    string newKey = "ConnectionString" ;

    string newValue = "ine";

    config.AppSettings.Settings.Add(newKey, newValue);

    // Save the configuration file.

    config.Save(ConfigurationSaveMode.Minimal);

    // Force a reload of the changed section.

    ConfigurationManager.RefreshSection("appSettings");

    }

    but it will change vshost.exe.config file and just untill closing the application. after restart nothing is changed.

    Monday, November 19, 2007 10:05 AM
  •  

    static void UpdateAppSettings()

    {

    //replace with your project name .exe file

    string configPath = System.IO.Path.Combine(Environment.CurrentDirectory, "YourProject.exe");

    System.Configuration.Configuration config =

    ConfigurationManager.OpenExeConfiguration(configPath);

    // Add an entry to appSettings.

    int appStgCnt =

    ConfigurationManager.AppSettings.Count;

    string newKey = "ConnectionString" ;

    string newValue = "ine";

    config.AppSettings.Settings.Add(newKey, newValue);

    // Save the configuration file.

    config.Save(ConfigurationSaveMode.Minimal);

    // Force a reload of the changed section.

    ConfigurationManager.RefreshSection("appSettings");

    }

    Monday, November 19, 2007 10:56 AM
  • You need to specify explicitly app config file name else it would take the default one .vshost.exe, try this code it should work

    Thanks

     

    Monday, November 19, 2007 10:59 AM
  • yes! thank you

    know it works and I can combine both methods to save all config files. but still a little problem. when the key is not exist, it's ok but when I have a key with the same name then the value will append to the old value.

    how can I replace the new value with the old one?

     

    Monday, November 19, 2007 11:31 AM
  • I just unmark as answer to don't loose attention

     

    Monday, November 19, 2007 12:19 PM
  • yes! the solution is to add the following line before adding and it won't throw exception if the key is not exist:

    config.AppSettings.Settings.Remove(KEYNAME);

     

    Monday, November 19, 2007 12:22 PM
  • It's work, thanks.
    Thursday, April 22, 2010 12:18 AM