在运行时创建新设置,并在重新启动后读取
本文关键字:重新启动 读取 设置 运行时 创建 新设置 | 更新日期: 2023-09-27 18:24:09
我想存储用户设置。它们是在运行时创建的,应该在重新启动应用程序后读取。
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();
对结果没有任何影响。我也尝试过这里描述的其他东西,但都不适用。
对你来说可能有点晚了,但对其他人来说有两部分。
- 保存新的UserSetting
- 启动时从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
稍晚:我现在也遇到了同样的问题,汤姆的回答是唯一有效的暗示。但由于缺少一些细节,我想与您分享我的解决方案。
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().中
我希望我能帮助别人。