How is the logic behind storing program setting in text file like 'config.cfg'?

懵懂的女人 提交于 2019-12-06 04:08:24

Can you use standard .NET settings classes?

Advantages:
- you don't need to code for this
- you have UI to edit your default settings
- you reference settings in the code in a typed manner, like this Settings.Default.Test1.
- your settings will be stored in the application config file, which is in XML.

More details here: http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx.

In config file it would look like this:

  <setting name="Test2"
           serializeAs="String">
    <value>My string setting</value>
  </setting>

You might argue that XML is not easy to read for the end user. But I believe that users who understand what 'Voltage Offset' is, should not have issues reading/updating settings in xml file :).

Actually the easiest way for you to do it in a .net environment is to have xml settings file. Using an xmlserializer you can serialize an object to text and deserealize from text.

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Edit: also xmlserializer won't serialize private fields/properties, so you should use public properties to get the desired result.

The setting are usually stored in one of the common formats E.g. XML (very common now), INI (used to be common), LAU, ...

Serializing object to these formats or deserializing from them can be done with built in serializers (e.g. XmlSerializer in .NET), 3rd party ones or even propriety custom ones.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!