下次如何加载动态创建的设置属性?
我创建了一种将 SettingsProperty
动态添加到 .NET app.config
文件的方法。 这一切都运行良好,但是当我下次启动我的应用程序时,我只能看到在设计器中创建的属性。 如何加载运行时属性?
我用于创建 SettingsProperty
的代码如下所示:
internal void CreateProperty<T>(string propertyName)
{
string providerName = "LocalFileSettingsProvider";
System.Configuration.SettingsAttributeDictionary attributes = new SettingsAttributeDictionary();
System.Configuration.UserScopedSettingAttribute attr = new UserScopedSettingAttribute();
attributes.Add(attr.TypeId, attr);
System.Configuration.SettingsProperty prop;
SettingsProvider provider = ApplicationEnvironment.GlobalSettings.Providers[providerName];
prop = new System.Configuration.SettingsProperty(
propertyName,
typeof(T),
provider,
false,
default(T),
System.Configuration.SettingsSerializeAs.String,
attributes,
false,
false
);
ApplicationEnvironment.GlobalSettings.Properties.Add(prop);
ApplicationEnvironment.GlobalSettings.Reload();
}
当下次运行时,我询问设置属性,但找不到之前创建的任何属性。 无论我是否调用 ApplicationEnvironment.GlobalSettings.Reload();
。
I created a way to dynamically add SettingsProperty
to a .NET app.config
file. It all works nicely, but when I am launching my app the next time I can only see the properties that are created in the designer. How can I load back the properties runtime?
My code for creating the SettingsProperty
looks like the following:
internal void CreateProperty<T>(string propertyName)
{
string providerName = "LocalFileSettingsProvider";
System.Configuration.SettingsAttributeDictionary attributes = new SettingsAttributeDictionary();
System.Configuration.UserScopedSettingAttribute attr = new UserScopedSettingAttribute();
attributes.Add(attr.TypeId, attr);
System.Configuration.SettingsProperty prop;
SettingsProvider provider = ApplicationEnvironment.GlobalSettings.Providers[providerName];
prop = new System.Configuration.SettingsProperty(
propertyName,
typeof(T),
provider,
false,
default(T),
System.Configuration.SettingsSerializeAs.String,
attributes,
false,
false
);
ApplicationEnvironment.GlobalSettings.Properties.Add(prop);
ApplicationEnvironment.GlobalSettings.Reload();
}
When the next run I ask for the settings property I could not find any of the properties created previosuly. No matter if I call the ApplicationEnvironment.GlobalSettings.Reload();
or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户定义的配置设置与创建它们所用的程序集版本相关联。 如果您有滚动版本号(例如 1.0..),您将丢失上次运行的设置。
User defined configuration settings are tied to the assembly version they were created with. If you have a rolling version number (1.0.. for example), You will lose the settings from the last run.
我遇到了同样的问题。 恕我直言,问题在于 .NET System.Configuration.SettingsBase 对象使用反射来确定应从持久存储加载的属性的名称、类型等。 当您添加动态设置属性时,此信息会丢失。 因此,您不仅需要在保存值之前添加设置属性定义,而且还需要在读取值之前添加设置属性定义。 在您的情况下,它应该是这样的
您可能希望在开始时为要使用的所有属性调用
CreateProperty
方法,然后仅调用Reload
一次。 在这两种情况下,您都需要知道属性的名称和类型。I came across the same issue. IMHO the problem is that the .NET
System.Configuration.SettingsBase
object uses reflection to determine the names, types, etc. of the properties that should be loaded from the persistent storage. And when you add a dynamic settings property, this information is missing. So you'll need to add the settings property definition not only before you save the value, but also before you read it. In your case, it should be something like thisYou may want to call the
CreateProperty
method at the beginning for all the properties you want to use and then callReload
just once. In both cases, you'll need to know the names and types of the properties.