0

I have to pretty simple pieces of code:

    private void frmMain_Load(object sender, EventArgs e)
    {
        string[] EmployeeNames = Settings.Default.Employee_Names.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
        employee_NameComboBox.Items.AddRange(EmployeeNames);

    }

And

    private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        string Employee_Names = string.Empty;
        foreach (string Name in employee_NameComboBox.Items)
        {
            Employee_Names += Name + ";";
        }
        if (Employee_Names.Length > 1)
        {
            Settings.Default.Employee_Names = Employee_Names.Substring(0, Employee_Names.Length - 1);
            Settings.Default.Save();
        }


    }

So, to test my code I added the values "Foo;Bar" To my Settings.Settings in the VS Designer but when I run the Code Nothing comes up. So I add this code:

    private void AddName()
    {
        if (!employee_NameComboBox.Items.Contains(employee_NameComboBox.Text))
        {
            employee_NameComboBox.Items.Add(employee_NameComboBox.Text);
        }
    }

And then I call it on the Leave event of employee_NameComboBox:

    private void employee_NameComboBox_Leave(object sender, EventArgs e)
    {
        AddName();
    }

To test this I enter the employee_NameComboBox and type in "Employee1" and leave the CB then enter again and type in "Employee2" and Leave now when I click on the drop down I have these strings in the Items Collection. Great!

But when I close the application and then goto my settings.settings under properties in the Solution Explorer the values "Foo;Bar" are still there.

Continue to start Debugging again and when I click the drop down, the values are "Employee1" and "Employee2". In turn I search through the Solutions Directory in Windows Explorer to try and find the new settings file and I simply CAN NOT find ANY files with "Employee1" or "Employee2" all I see in ANY files is "Foo;Bar".

How can this be? I am saving to somewhere but where!

Also Why doesn't the debugger use the values in the settings.settings or App.Config files?!?!?!

Note: I also tried doing a Find from Visual Studios and Searched Entire Solution for the value Employee1 to no avail.

My end result is that I would like to send out an application with editable combobox that can be saved and edited without a Re-compile.

user2140261
  • 7,855
  • 7
  • 32
  • 45

2 Answers2

2

When such setting are changed at run-time, the values are stored in the following directory:

  C:\Documents and Settings\<username>\Local Settings\Application Data

That's where the app then goes and looks for them the next time.

0

The settings are overwritten on each build by whatever you have set up in VS. If you want to use the settings from a previous compilation (or once deployed to a client machine, a previous version after an update) you have to use Settings.Upgrade(). This should be called once per runtime, most easily in the constructor. See this other StackOverflow question. C# .NET Application Settings and Upgrading

Community
  • 1
  • 1
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40