c#: Create new settings at run time

前端 未结 8 786
粉色の甜心
粉色の甜心 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["<baseSetting>"]);
    property.Name = "<dynamicSettingName>";
    Settings.Default.Properties.Add(property);
    // will have the stored value:
    var dynamicSetting = Settings.Default["<dynamicSettingName>"];
    

    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.

    0 讨论(0)
  • 2020-12-10 02:16

    It took me a long time using the top two answers here plus this link (Create new settings on runtime and read after restart) to get it to finally work.

    First of all, set your expectations. The answer here will create a new user setting and you can get its value the next time you launch your app. However, the setting you created this way will not appear in the Settings designer. In fact, when you relaunch the app and try to access the setting in your code, it will not find it. However, the setting you have created through code is saved in the user.config file (say jDoe.config) somewhere in your file system. For you to access this value, you have to add the setting again.

    Here is a working example I have:

            private void FormPersistence_Load(object sender, EventArgs e)
            {
                StartPosition = FormStartPosition.Manual;
                // Set window location
                var exists = Settings.Default.Properties.OfType<SettingsProperty>().Any(p => p.Name == Name + "Location");
                if (exists)
                {
                    this.Location = (Point)Settings.Default[Name + "Location"];
                }
                else
                {
                    var property = new SettingsProperty(Settings.Default.Properties["baseLocation"]);
                    property.Name = Name + "Location";
                    Settings.Default.Properties.Add(property);
                    Settings.Default.Reload();
                    this.Location = (Point)Settings.Default[Name + "Location"];
                }
            }
    

    Note: My new setting's name will be resolved at run time. Name is really this.Name, which is the form's name. This is a base form that other forms can inherit from, so all the child forms will be able to remember their locations.
    baseLocation is a setting I have manually created in Settings designer. The new setting I have is the same type. This way I don't have to worry about things like provider, type, etc. in code.

    0 讨论(0)
提交回复
热议问题