How to save user settings programmatically?

我的梦境 提交于 2019-12-05 09:00:00

问题


I have a button which opens windows color pallet and then assigns select color to selected element in some virtual studio. Element is first selected by the user on mouse click, and based on element ID, color is assigned. So, each time the button is clicked, color of the same or different element is changed. Element ID is obtained from a delegate that fires if mouse clicked on some element. The code for color-set button is like this:

private void Btn_Choose_Color_Click(object sender, RoutedEventArgs e)
{
    uint id_selected = (uint)selected_element; //get id of selected element from clickintocallback

    //open windows color dialog
    System.Windows.Forms.ColorDialog my_dialog = new System.Windows.Forms.ColorDialog();
    my_dialog.ShowDialog();

    //get the color from windows dialog
    int red = my_dialog.Color.R;
    int green = my_dialog.Color.G;
    int blue = my_dialog.Color.B;

    //create cinector color object and pass rgb values from windows dialog
    ByteRGBColor desired_color = new ByteRGBColor((byte)red, (byte)green, (byte)blue); //assign color statically

    for (int i = 0; i < all_color_elements_in_loaded_studio.Count; i++)
    {
        uint id_current = all_color_elements_in_loaded_studio.ElementAt(0).colorElementID; //get id of current element in a loop

        if(id_current == id_selected) //compare selected and current element
        {
            //all_color_elements_in_loaded_studio.ElementAt(i).colorElementColor = test_color; //set the test color
            instance.SetStudioColorElement(id_current, desired_color); //assign the color to the element
            break;
        }
    }

    //after we choose a color
    Btn_Pick_Element_Clicked = false;
    Btn_Choose_Color.IsEnabled = false;
}

Now, my question is how to save element ID and its color once assigned into user settings? I understand that I can go to Properties->Settings and manually define user settings there, but here it must be done somehow programmatically. And then also, how to load these settings back?


回答1:


Set

Properties.Settings.Default.myColor = Color.AliceBlue;

Get

this.BackColor = Properties.Settings.Default.myColor;

Save

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();

Reference




回答2:


Take a look at this article, especially at Saving User Settings at Run Time section.

To have this answer not just be a link. Here is the relevant section reproduced:

Saving User Settings at Run Time

Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file. To Write and Persist User Settings at Run Time Access the user setting and assign it a new value, as shown in the following example:

Properties.Settings.Default.myColor = Color.AliceBlue;

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();


来源:https://stackoverflow.com/questions/31748789/how-to-save-user-settings-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!