如何在Properties.Settings.Default.Properties中循环修改值

本文关键字:Properties 循环 修改 Settings Default | 更新日期: 2023-09-27 17:54:21

我有以下代码:

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

如何在Properties.Settings.Default.Properties中循环修改值

这可能行得通:

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

请记住,属性应该有'User'作用域,以便保存

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

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(); 
        } 
    } 

我想在程序启动时检索设置并保存它们,以便整个设置文件可以在appData中编辑。这是我在启动程序时调用的:

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

无论设置类型如何,这似乎都有效。