C# 如何以编程方式保存用户设置?

C# 如何以编程方式保存用户设置?,c#,.net,settings,C#,.net,Settings,我有一个按钮,可以打开windows颜色托盘,然后将“选择颜色”指定给某个虚拟工作室中的选定元素。用户在单击鼠标时首先选择元素,并根据元素ID指定颜色。因此,每次单击按钮时,相同或不同元素的颜色都会改变。元素ID是从一个委托中获取的,如果鼠标单击某个元素,该委托将触发。颜色设置按钮的代码如下所示: private void Btn_Choose_Color_Click(object sender, RoutedEventArgs e) { uint id_selected = (uint

我有一个按钮,可以打开windows颜色托盘,然后将“选择颜色”指定给某个虚拟工作室中的选定元素。用户在单击鼠标时首先选择元素,并根据元素ID指定颜色。因此,每次单击按钮时,相同或不同元素的颜色都会改变。元素ID是从一个委托中获取的,如果鼠标单击某个元素,该委托将触发。颜色设置按钮的代码如下所示:

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;
}
private void Btn\u选择\u颜色\u单击(对象发送者,路由目标)
{
uint id_selected=(uint)selected_元素;//从clickintocallback获取所选元素的id
//打开windows颜色对话框
System.Windows.Forms.ColorDialog my_dialog=新建System.Windows.Forms.ColorDialog();
my_dialog.ShowDialog();
//“从windows获取颜色”对话框
int red=my_dialog.Color.R;
int green=my_dialog.Color.G;
int blue=my_dialog.Color.B;
//“创建cinector颜色对象并从windows传递rgb值”对话框
所需ByteRGBColor_color=新ByteRGBColor((字节)红色,(字节)绿色,(字节)蓝色);//静态分配颜色
for(int i=0;i
现在,我的问题是,一旦分配到用户设置中,如何保存元素ID及其颜色?我知道我可以转到属性->设置并在那里手动定义用户设置,但这里必须以编程方式完成。然后,如何重新加载这些设置?

Set

Properties.Settings.Default.myColor = Color.AliceBlue;
this.BackColor = Properties.Settings.Default.myColor;
获取

Properties.Settings.Default.myColor = Color.AliceBlue;
this.BackColor = Properties.Settings.Default.myColor;
保存

如果要在应用程序会话之间保留对用户设置的更改,请调用Save方法,如以下代码所示:

Properties.Settings.Default.Save();
Properties.Settings.Default.Save();

看一看,尤其是在运行时保存用户设置部分

得到这个答案不仅仅是一个链接。以下是相关章节:

在运行时保存用户设置

应用程序作用域设置是只读的,只能在设计时或在应用程序会话之间更改.exe.config文件来更改。但是,用户范围设置可以在运行时写入,就像更改任何属性值一样。新值在应用程序会话期间保持不变。通过调用
settings.Save
方法,可以在应用程序会话之间保留对用户设置的更改。这些设置保存在User.config文件中。 在运行时写入和保留用户设置 访问用户设置并为其分配新值,如以下示例所示:

Properties.Settings.Default.myColor = Color.AliceBlue;
如果要在应用程序会话之间保留对用户设置的更改,请调用Save方法,如以下代码所示:

Properties.Settings.Default.Save();
Properties.Settings.Default.Save();
可能需要该.Save()调用的副本。谢谢