为什么 ApplicationSettingsBase.Reset() 会清空 PropertyValues?
我认为 Reset() 方法会再次使用默认值重新填充设置,但似乎并非如此。如何使用默认值重新加载它们?
private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
FooSettings.Default.Reset();
// Data grid will show an empty grid after call to reset.
DataGridFoo.Rows.Clear();
foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
{
DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
}
}
更新
private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
foreach (SettingsProperty sp in FooSettings.Default.Properties)
{
FooSettings.Default[sp.Name.ToString()] = sp.DefaultValue;
}
DataGridFoo.Rows.Clear();
foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
{
DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
}
}
删除了对 Reset() 的调用,并将属性值手动设置为默认存储的值。我仍然很想知道这是否是应该使用的方式,还是我错过了什么?
I thought the Reset() method repopulates the Settings with default values again, but it seems not to. How can I reload them with the default values?
private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
FooSettings.Default.Reset();
// Data grid will show an empty grid after call to reset.
DataGridFoo.Rows.Clear();
foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
{
DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
}
}
Update
private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
foreach (SettingsProperty sp in FooSettings.Default.Properties)
{
FooSettings.Default[sp.Name.ToString()] = sp.DefaultValue;
}
DataGridFoo.Rows.Clear();
foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
{
DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
}
}
Removed the call to Reset() and set the property values manually to the default stored ones. I'm still eager to hear if this is the way it's supposed to be used or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到这个线程是因为我遇到了同样的问题。我想我应该向未来可能来这里的旅行者报告我的发现。我不能保证这是 100% 准确或完整的,因为我已经摆弄了它一个小时,这已经足够摆弄一天了,尽管我觉得还有更多东西需要了解。但至少他们会在这里提供一些提示。 :)
虽然
Reset()
的文档似乎表明 user.config 文件中保存的设置被 app.config 文件中的默认值覆盖,但事实似乎并非如此。它只是从 user.config 文件中删除设置,使用上面的示例,会导致FooSettings.Default.PropertyValues
的计数为 0,因为使用Reset()< 后不存在任何设置。 /代码>。但是有一些方法可以处理这个结果,而无需像OP那样重新填充设置。一种方法是显式检索单个设置值,如下所示:
其他方法涉及使用
SettingsPropertyValueCollection
和/或SettingsPropertyCollection
:因此,回到最初的问题,您可以做的是这:
I came across this thread because I encountered the same issue. I figured I'd report my findings for any future travelers who might come this way. I can't promise this is 100% accurate or complete 'cause I've been fiddling with it for an hour that's enough fiddling for one day even though I feel like there's still more to know. But at least they'll be some tips here. :)
Although the documentation for
Reset()
seems to indicate that saved settings are overwritten in the user.config file with the default values from the app.config file, this does not appear to be the case. It simply erases the settings from the user.config file, which, using the example above, results inFooSettings.Default.PropertyValues
having a count of 0 because none exists after usingReset()
. But there are ways to work with this result that don't necessitate repopulating the settings as the OP did. One way is to explicitly retrieve individual settings values like this:Other ways involve using
SettingsPropertyValueCollection
and/orSettingsPropertyCollection
:So, returning to the original question, what you could have done is this: