[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

c# changes to app.config lost after close

I have the following code that I use to update valu3es in app.config.

As the app runs I can see the changes are made (via the showSettings() ca. On a restart though all changes are lost and we are back to the original file.  

I have found this class confusing in the past and so usually just use my own XML file to save config settings, but I would like to undestand why it doesn't work as it would be good to use.

private void TB_IPAddress_Leave(object sender, EventArgs e)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["jeqi.ip"].Value = TB_IPAddress.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            FJeqiChanged = true;

            showSettings();



        }

        private void TB_Port_Leave(object sender, EventArgs e)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["jeqi.port"].Value = TB_Port.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            FJeqiChanged = true;

            showSettings();
        }



Any ideas
private void showSettings()
        {
            String value = "";
            foreach (string key in ConfigurationManager.AppSettings)
            {
                value = value + ConfigurationManager.AppSettings[key] + "\n"; ;
            }
            MessageBox.Show(value, "settings");
        }

Open in new window

0
jetbet
Asked:
jetbet
1 Solution
 
OklahomaDaveCommented:
I would fire up a copy of ProcMon to see if the configuration file *you* think is being modified, then reverting, is really the one you believe it to be. I'm making a guess that what's happening is that the changes you see are being made to a temporary copy of the file, not the "real" copy in the application folder. If my theory is anywhere near right, this will be readily apparent in procmon.

I may be completely off base here, but its a shot.
0
 
jetbetAuthor Commented:
Sorry for the slow response.

You may be right, although I have heard that updaes can be troublesome for the config files.

I have decided to just use an XML file as this is easier and does not have the un-needed complexity that the App.Config file has.

I will assign you the points as you are the only person to offer help.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now