What's the best way to import/export app internal settings into a file from within an app?
I have the Settings.settings file, winform UI tied to the settings file, and I want to import/export settings, similar to Visual Studio Import/Export Settings feature.
You could just use sections, or are you breaking out to other files for a specific reason?
If you are using the Settings.settings file, it's saving to the config file. By calling YourNamespace.Properties.Settings.Save() after updating your settings, they will be saved to the config files.
However, I have no idea what you mean by "multiple sets of settings." If the settings are user settings, each user will have its own set of settings. If you are having multiple sets of settings for a single user, you probably should not use the .settings files; instead you'll want to use a database.
A tried and tested way I have used is to design a settings container class. This container class can have sub-classes for different types of setting categories. It works well since you reference your "settings" via property name and therefore if something changes in future, you will get compile time errors. It is also expandible, since you can always create new settings by adding more properties to your individual setting classes and assign default values to the private variable of a property that will be used should that specific setting not exist in an older version of your application. Once the new container is saved, the new settings will be persisted as well. Another advantage is the obvious human / computer readability of XML which is nice for settings.
To save, serialize the container object to XML data, then write the data to file. To load, read the data from file and deserialize back into your settings container class.
To serialize via standard C# code:
To deserialize via standard C# code:
Once last nice thing about a serializable settings class is because it is an object, you can use IntelliSense to quickly navigate to a particular setting.
Note: After you instantiated your settings container class, you should make it a static property of another static managing class (you can call it SettingsManager if you want) This managing class allows you to access your settings from anywhere in your application (since its static) and you can also have static functions to handle the loading and saving of the class.
You can use DataSet, which you bind to the form. And you can save/restore it.
Are you talking about configuration files?