简体   繁体   English

C#:从文件导入/导出设置

[英]C#: Import/Export Settings into/from a File

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.我有 Settings.settings 文件,winform UI 绑定到设置文件,我想导入/导出设置,类似于 Visual Studio 导入/导出设置功能。

If you are using the Settings.settings file, it's saving to the config file.如果您使用的是 Settings.settings 文件,它会保存到配置文件中。 By calling YourNamespace.Properties.Settings.Save() after updating your settings, they will be saved to the config files.通过在更新您的设置后调用 YourNamespace.Properties.Settings.Save(),它们将被保存到配置文件中。

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;如果您为单个用户设置多组设置,您可能不应该使用 .settings 文件; instead you'll want to use a database.相反,您需要使用数据库。

You can use DataSet, which you bind to the form.您可以使用绑定到表单的数据集。 And you can save/restore it.您可以保存/恢复它。

您可以只使用部分,还是由于特定原因而中断到其他文件?

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.另一个优点是 XML 明显的人类/计算机可读性,这对设置非常有用。

To save, serialize the container object to XML data, then write the data to file.要保存,请将容器对象序列化为 XML 数据,然后将数据写入文件。 To load, read the data from file and deserialize back into your settings container class.要加载,请从文件中读取数据并将其反序列化回您的设置容器类。

To serialize via standard C# code:通过标准 C# 代码序列化:

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:通过标准 C# 代码反序列化:

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.关于可序列化设置类的最后一个好处是因为它是一个对象,您可以使用 IntelliSense 快速导航到特定设置。

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.注意:在实例化设置容器类后,您应该将其设为另一个静态管理类的静态属性(如果需要,您可以将其称为 SettingsManager)该管理类允许您从应用程序的任何位置访问您的设置(因为它是静态的) 并且您还可以使用静态函数来处理类的加载和保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM