3

I have this rater simple application, where I use settings. For that, I have created my own Properties settings file. So far so good. That works all well. The settings are there and I can use them.

I am trying to be able to change these settings at runtime. From the looks of it, that also works quite well. Looking at the user.config for the application under the user running the application, settings ARE being saved. However, the next time I load the application, it seems to ignore the saved settings and use the default ones. Once again I can change the the settings and save them, with the same result. The user.config is being updated, but the old default settings are always the once being shown on load.

I load the settings into a ListView. Change the values from there, and then loops through the ListView, saving the settings again.

This is where I load the settings in the ListView: (Properties.Main is the settings I have created)

private void Settings_Load(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
    textBox1.Text = config.FilePath;

    foreach (SettingsProperty Setting in Properties.Main.Default.Properties)
    {
        ListViewItem settingitem = new ListViewItem();

        settingitem.Text = Setting.Name.ToString();
        settingitem.SubItems.Add(Setting.DefaultValue.ToString());

        listView1.Items.Add(settingitem);

    }
    listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}

And this is where I save the settings.

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        foreach (ListViewItem setting in listView1.Items)
        {
            Properties.Main.Default[setting.Text] = setting.SubItems[1].Text; ;
            Properties.Main.Default.Save();
        }
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
    }
}

From what I can see, the settings are being saved as expected in the user.config file, but it is not the one being loaded the next time.

I am getting the impression, that there is something I need to do, in order for the application not to load the default settings each time.

Any ideas?

Kara
  • 6,115
  • 16
  • 50
  • 57
  • Of note, Visual Studio likes to mess with settings so if this is a problem you are seeing when running your app out of the development environment it may just be an artifact of that. – Yaur Jun 13 '12 at 04:26
  • I have actually been thinking about it. Therefor it is also tested. I've tested it on 3 different pc's, and non of them uses the saved settings. – stallemanden Jun 13 '12 at 04:45
  • Where are you change setting values? according to your code you set previous value again – Damith Jun 13 '12 at 05:30
  • I figured it was not important to add the code for the popup I have made. Once I click a setting in the ListView, i get a popup. There I change the value of the setting and apply it to the ListView in the right place. This is tested and works. When I loop the listitems for saving them, I have even tried showing the result of setting. This shows what I am expecting. `Properties.Main.Default[setting.Text] = setting.SubItems[1].Text; Properties.Main.Default.Save(); MessageBox.Show(Properties.Main.Default[setting.Text].ToString());` – stallemanden Jun 13 '12 at 05:40
  • Also having this problem – Jacob Alley May 24 '18 at 15:33

1 Answers1

0

Have you tried ConfigurationManager.RefreshSection

astro boy
  • 1,410
  • 1
  • 11
  • 16
  • From what I can understand, this is used if there is an issue with the settings not being written to the disk. However, this is not the case. Also adding it, does not help unfortunately. – stallemanden Jun 13 '12 at 04:18