سؤال

There are a handful of default listed data types in the Settings.settings file, and then you can add some more from a tree when you select "Browse".

I actually need to store an int[] in my settings file, and have done it this way:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public int[] SPLTimings
{
    get
    {
        return ((int[])(this["SPLTimings"]));
    }
    set
    {
        this["SPLTimings"] = value;
    }
}

and on first run of the program:

int[] timings = Settings.SPLTimings ?? new int[] { 25, 100, 80, 50, 125, 50 };

which later gets saved to disk. with Settings.Save()

This works quite well, but there's a small problem with it. If I want to add a simple data type using the settings GUI interface, then my custom additions get deleted and I have to add them again. I currently only have this one special case, so it's not a big deal to add it if it gets deleted. On the other hand, I can just add subsequent basic settings like strings/ints/etc in the same way I added the int[]: coding it directly into the Settings.Designer file.

There must be a better way to do this?

Since the Settings class is partial already, would it be a better solution to create a new file, say CustomSettings, which continues the class definition and includes these obscure types?

هل كانت مفيدة؟

المحلول

Settings.Designer.cs is generated automatically. So yes, you can use the fact that this class is partial, and define your own settings it another file with the same class definition. This way settings will not be overwriten.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top