【问题标题】:Create new settings on runtime and read after restart在运行时创建新设置并在重启后读取
【发布时间】:2014-11-19 01:08:18
【问题描述】:

我想存储用户设置。它们是在运行时创建的,应该在重新启动应用程序后读取。

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    var property = new SettingsProperty("Testname");
    property.DefaultValue = "TestValue";
    Settings.Default.Properties.Add(property);
    Settings.Default.Save();
}

此时,设置已存储,我可以访问它。

重启应用后,新创建的设置就消失了:

public MainForm()
{
    InitializeComponent();

    foreach (SettingsProperty property in Settings.Default.Properties)
    {
          //Setting which was created on runtime before not existing
    }
}

尝试这件作品:Settings.Default.Reload(); 对结果没有任何影响。我还尝试了其他类似here 的方法,但它们都不适合我。

【问题讨论】:

    标签: c# winforms settings


    【解决方案1】:

    稍等片刻: 我现在遇到了同样的问题,汤姆的回答是唯一有效的提示。但是由于它缺少一些细节,我想与您分享我的解决方案。

    using System.Configuration;
    
    public static class ApplicationSettingsBaseExtension
        {
            public static void Add<T>(this ApplicationSettingsBase settings, string propertyName, T val)
            {
    
                bool itemExists = false;
                foreach (SettingsProperty property in settings.Properties)
                {
                    if (property.Name == propertyName)
                    {
                        itemExists = true;
                        break;
                    }
                }
                if (!itemExists)
                {
                    var p = new SettingsProperty(propertyName)
                    {
                        PropertyType = typeof(T),
                        Provider = settings.Providers["LocalFileSettingsProvider"],
                        SerializeAs = SettingsSerializeAs.Xml
                    };
    
                    p.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
    
                    settings.Properties.Add(p);
                    settings.Reload();
                }
    
                settings[propertyName] = val;
                settings.Save();
            }
    
            public static T Get<T>(this ApplicationSettingsBase settings, string propertyName, T defaultValue)
            {
                bool itemExists = false;
                foreach (SettingsProperty property in settings.Properties)
                {
                    if (property.Name == propertyName)
                    {
                        itemExists = true;
                        break;
                    }
                }
                if (!itemExists)
                {
                    var p = new SettingsProperty(propertyName)
                    {
                        PropertyType = typeof(T),
                        Provider = settings.Providers["LocalFileSettingsProvider"],
                        SerializeAs = SettingsSerializeAs.Xml
                    };
    
                    p.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
    
                    settings.Properties.Add(p);
                    settings.Reload();
                }
                //finally set value with new value if none was loaded from userConfig.xml
                var item = settings[propertyName];
                if (item == null)
                {
                    settings[propertyName] = defaultValue;
                    settings.Save();
                }
    
                return (T)settings[propertyName];
            }
    
    
        }
    

    你可以使用:

    Properties.Settings.Default.Add(settingName, settingValue);
    Properties.Settings.Default.Save();
    ...
    setting = Settings.Default.Get(settingName, "");
    

    在我的例子中,“设置”是一个字符串,但它应该适用于所有基本类型。 请注意,这些设置不包含在 Settings.Default.Upgrade() 中。

    我希望我能帮助别人。

    【讨论】:

      【解决方案2】:

      对你来说可能有点晚,但对其他人来说有 2 个部分。

      1. 保存新的 UserSetting
      2. 启动时从 userConfig.xml 重新加载

      我根据其他答案为 ApplicationSettingsBase 创建了这个扩展

      public static void Add<T>(this ApplicationSettingsBase settings, string propertyName, T val)
      {           
          var p = new SettingsProperty(propertyName)
          {
              PropertyType = typeof(T),
              Provider = settings.Providers["LocalFileSettingsProvider"],
              SerializeAs = SettingsSerializeAs.Xml
          };
      
          p.Attributes.Add(typeof(UserScopedSettingAttribute), new UserScopedSettingAttribute());
      
          settings.Properties.Add(p);
          settings.Reload();
      
          //finally set value with new value if none was loaded from userConfig.xml
          var item = settings[propertyName];
          if (item == null)
          {
              settings[propertyName] = val;
              settings.Save();
          }
      }
      

      这将使 Settings["MyKey"] 工作,但是当您重新启动设置时不会加载,但 userConfig.xml 具有新值(如果您调用 Settings.Save())

      让它重新加载的技巧是再次执行 Add 例如

      if (settings.Properties.Cast<SettingsProperty>().All(s => s.Name != propertyName))
      {
          settings.Add("MyKey", 0);
      };
      

      Add 的工作方式是,如果没有从 userConfig.xml 加载任何值,它只会将 MyKey 设置为 0

      【讨论】:

      • 有点晚了,但这看起来不错。我不会尝试,但我会给你一个 +1 的努力,谢谢
      • 这很酷,但需要注意的是,在启动时调用 Settings.Default.Upgrade() 在你添加动态设置之前,将意味着动态设置不会被携带前进到新版本。
      • @Tom Deloford,你能澄清一下“让它重新加载的技巧是再次执行 Add ”以及我们需要在哪里添加?
      • 我不明白为什么最后一段代码是必要的以及如何使用它。请添加更多信息。谢谢。
      猜你喜欢
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2018-09-13
      • 2020-05-29
      相关资源
      最近更新 更多