C#如何遍历Properties.Settings.Default.Properties更改值

我有以下代码:

foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if (Double.TryParse(GenerateValue()), out result)) { currentProperty.DefaultValue = result.ToString(); Properties.Settings.Default.Save(); } } 

它从mysql数据库中获取新值。 如果我添加一个MessageBox.Show来显示新值,它似乎工作正常但它实际上并没有保存它。 我认为这是因为我将值赋给变量…是否有某种方法可以做到这一点?

 Properties.Settings.Default.IndexOf(currentProperty.name).DefaultValue = result 

这可能有效:

 foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { Properties.Settings.Default[currentProperty.Name] = result.ToString(); Properties.Settings.Default.Save(); } 

请记住,属性应具有“用户”范围才能保存。

我同意你的结论。 你要做的是通过字符串值获取属性。

 Properties.Settings.Default[string value] = foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if (Double.TryParse(GenerateValue()), out result)) { Properties.Settings.Default[ currentProperty.Name ] = result.ToString(); Properties.Settings.Default.Save(); } } 

以上是你真正想要的。