C#: Import/Export Settings into/from a File

2019-07-12 12:11发布

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.

5条回答
\"骚年 ilove
2楼-- · 2019-07-12 12:23

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

查看更多
\"骚年 ilove
3楼-- · 2019-07-12 12:32

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.

查看更多
The star\"
4楼-- · 2019-07-12 12:32

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:

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 standard C# 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.

查看更多
smile是对你的礼貌
5楼-- · 2019-07-12 12:36

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

查看更多
劳资没心,怎么记你
6楼-- · 2019-07-12 12:43

Are you talking about configuration files?

查看更多
登录 后发表回答