c#:在运行时创建新设置 [英] c#: Create new settings at run time

查看:76
本文介绍了c#:在运行时创建新设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#Windows窗体:如何在运行时创建新设置,以便将它们永久保存为Settings.Default。-值?

c# windows forms: How do you create new settings at run time so that they are permanently saved as Settings.Default.-- values?

推荐答案

以防万一仍然对任何人都重要:

Just in case that still matters to anyone:

您可以通过 Settings.Default.Properties.Add( ...),并在保存后将它们也保留在本地存储中(我将那些条目反映在漫游文件中。)

You can dynamically add settings through Settings.Default.Properties.Add(...) and have these also persisted in the local storage after saving (I had those entries reflected in the roaming file).

不过重新加载后,动态添加的设置似乎在 Settings.Default.Properties 集合中仍然丢失。

Nevertheless it seems that the dynamically added settings keep missing in the Settings.Default.Properties collecion after loading again.

I可以通过在首次访问动态属性之前添加动态属性来解决此问题。
示例(注意,我从基本设置创建了动态设置):

I could work around this problem by adding the dynamic property before first accessing it. Example (notice that I "create" my dynamic setting from a base setting):

// create new setting from a base setting:
var property = new SettingsProperty(Settings.Default.Properties["<baseSetting>"]);
property.Name = "<dynamicSettingName>";
Settings.Default.Properties.Add(property);
// will have the stored value:
var dynamicSetting = Settings.Default["<dynamicSettingName>"];

我不知道Microsoft是否支持此功能,因为有关该主题的文档非常少。

I don't know if this is supported by Microsoft as the documentation is very rare on this topic.

问题也在此处描述 http://www.vbdotnetforums.com/vb-net-general-discussion/29805-my-settings-运行时已添加-properties-dont-save.html#post88152 ,此处提供了一些解决方案 http://msdn.microsoft.com/zh-cn/library/saa62613(v = VS.100).aspx (请参阅社区内容-标题如何创建/保存/加载动态(在运行时)设置)。但这是VB.NET。

Problem is also described here http://www.vbdotnetforums.com/vb-net-general-discussion/29805-my-settings-run-time-added-properties-dont-save.html#post88152 with some solution offered here http://msdn.microsoft.com/en-us/library/saa62613(v=VS.100).aspx (see Community Content - headline "How to Create / Save / Load Dynamic (at Runtime) Settings"). But this is VB.NET.

这篇关于c#:在运行时创建新设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆