下次如何加载动态创建的设置属性?

发布于 2024-07-06 20:20:40 字数 1211 浏览 9 评论 0原文

我创建了一种将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2024-07-13 20:20:41

用户定义的配置设置与创建它们所用的程序集版本相关联。 如果您有滚动版本号(例如 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.

忘你却要生生世世 2024-07-13 20:20:41

我遇到了同样的问题。 恕我直言,问题在于 .NET System.Configuration.SettingsBase 对象使用反射来确定应从持久存储加载的属性的名称、类型等。 当您添加动态设置属性时,此信息会丢失。 因此,您不仅需要在保存值之前添加设置属性定义,而且还需要在读取值之前添加设置属性定义。 在您的情况下,它应该是这样的

...
// the name and type of the property being read must be known at this point
CreateProperty<T>( propertyName );
ApplicationEnvironment.GlobalSettings.Reload();
T propertyValue = ApplicationEnvironment.GlobalSettings[propertyName];

您可能希望在开始时为要使用的所有属性调用 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 this

...
// the name and type of the property being read must be known at this point
CreateProperty<T>( propertyName );
ApplicationEnvironment.GlobalSettings.Reload();
T propertyValue = ApplicationEnvironment.GlobalSettings[propertyName];

You may want to call the CreateProperty method at the beginning for all the properties you want to use and then call Reload just once. In both cases, you'll need to know the names and types of the properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文