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.

4

2 に答える 2

2

このような設定が実行時に変更されると、値は次のディレクトリに保存されます。

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

そこでアプリが移動し、次回それらを探します。

于 2013-03-08T17:51:32.150 に答える
0

設定は、VSで設定したものによってビルドごとに上書きされます。以前のコンパイル(またはクライアントマシンにデプロイされた後、更新後の以前のバージョン)の設定を使用する場合は、Settings.Upgrade()を使用する必要があります。これは、ランタイムごとに1回呼び出す必要があり、コンストラクターで最も簡単に呼び出すことができます。この他のStackOverflowの質問を参照してください。C#.NETアプリケーションの設定とアップグレード

于 2013-03-08T17:49:09.233 に答える