views:

1633

answers:

5

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.

A: 

You can use DataSet, which you bind to the form. And you can save/restore it.

Sunny
A: 

You could just use sections, or are you breaking out to other files for a specific reason?

CodeRot
A: 

Are you talking about configuration files?

Bob Chatman
Yes, operating directly on app.config files would work for me. I've seen some code for this using System.Configuration.ExeConfigurationFileMap but haven't made it work yet.
+2  A: 

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.

Austin Salonen
A: 

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 standerd dotnet code:

public static string SerializeToXMLString(object ObjectToSerialize) MemoryStream mem = new MemoryStream(); System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType()); ser.Serialize(mem,ObjectToSerialize); ASCIIEncoding ascii = new ASCIIEncoding(); return ascii.GetString(mem.ToArray());

To deserialize via standerd dotnet code:

public static object DeSerializeFromXMLString(System.Type TypeToDeserialize, string xmlString) byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString); MemoryStream mem = new MemoryStream(bytes); System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(TypeToDeserialize); return ser.Deserialize(mem);

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.

@Poltergoose: If I had enough reputation I'd edit your post to clean up the source code! Remember, 4 spaces out front to make it a code block.
sixlettervariables
Do you mean something like this by the settings container class?public class SettingsContainer{ public string StringSetting { get { return Settings.Default.StringSetting; } set { Settings.Default.StringSetting = value; } }}
This would be great if it was autogenerated. Another question is, settings/config files are already XML, so is MS not giving enough access and that way preventing directly operating on those XML files?