我创建了一种动态添加SettingsProperty
到 .NETapp.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();
。