Skip to main content
 首页 » 编程设计

c#之如何将设置保存到项目设置选项卡

2024年02月05日13bjzhanghao

在设置选项卡的项目属性中,我添加了值*.*。 然后我添加了另一个 Setting1 并添加到它的值 c:\

然后在 form1 的构造函数中:

textBox2.Text = (string)Properties.Settings.Default["Setting"]; 
textBox3.Text = (string)Properties.Settings.Default["Setting1"]; 

我希望每次用户在其中一个文本框中键入内容时,它都会将其保存到设置中。

private void textBox2_TextChanged(object sender, EventArgs e) 
{ 
    Properties.Settings.Default["Setting"] = textBox2.Text; 
} 
 
private void textBox3_TextChanged(object sender, EventArgs e) 
{ 
    Properties.Settings.Default["Setting1"] = textBox3.Text; 
} 

但每次我运行我的程序时,我都会得到第一个设置 *.* 和 c:\。

请您参考如下方法:

这是因为您没有保存对 Properties.Settings 属性所做的更改。要保存更改,您必须执行以下操作:

Properties.Settings.Default.Save();