c#: Create new settings at run time

前端 未结 8 792
粉色の甜心
粉色の甜心 2020-12-10 01:44

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

8条回答
  •  死守一世寂寞
    2020-12-10 02:16

    Just in case that still matters to anyone:

    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).

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

    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[""]);
    property.Name = "";
    Settings.Default.Properties.Add(property);
    // will have the stored value:
    var dynamicSetting = Settings.Default[""];
    

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

    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.

提交回复
热议问题