(.Net) suggestions on making a config file for a program?

╄→尐↘猪︶ㄣ 提交于 2020-01-20 04:26:07

问题


I'm not necessarily referring to app.configs, but a custom configuration file that would store my program's state whenever a user hits a "save" button.

In my example, it's a UI builder that allows the user to choose which "fields" are to be displayed on the left and right columns in a two-column screen, along with the order of the fields in each column. There's other stuff too, but you get the idea.

The user could open any configuration file they've saved and populate the UI builder appropriately.

I'm thinking this could be stored in an XML file, but I was wondering what sort of suggestions people would have on how to write it out, how to read from it (use LINQ maybe?) and other general approaches, including versioning etc. I'm sure this has come up before and I'd like to hear about best practices. Thanks!


回答1:


The method I use for my program is to keep my configuration values in a class that uses a DataTable in a DataSet to store values. When I want to save it, I use DataSet.WriteXML() to whatever filename the user wants.

My current use is pretty simple (one DataTable with key, value columns) but it easily expands to multiple related DataTables with specific columns for various information. The DataSet handles the file io.

Then it's a breeze to read back in with DataSet.ReadXML().

An advantage of this method over serializing classes is that it is much easier to handle files saved from a previous version of your program. You can set a value in the table for the version of the program used to save it, so your newer version will know the older file won't have any values added in the newer version - and it can set them appropriately to update the file version.

If you add new features to a class (values, methods) it's serialized file will be different than earlier versions - not sure how easy it would be to handle those older files.

Here is a simple example class.

Used like this, makes a file like this:

To create a new one:
      Dim UIcfg As UIsettings = New UIsettings("TestSettings.cfg")

      UIcfg.setGeneralValue("Version", "1.0.0")
      UIcfg.setGeneralValue("Author", "Bobs YourUncle")

      UIcfg.setFieldValues("FirstName", "Left", "1")
      UIcfg.setFieldValues("LastName", "Right", "1")
      UIcfg.setFieldValues("ShoeSize", "Left", "2")
      UIcfg.setFieldValues("ShoeColor", "Left", "3")

      UIcfg.Save()

To get values from it:
      Dim value As String = ""
      Dim values As String = ""
      value = UIcfg.getGeneralValue("Author")
      values = UIcfg.getFieldValues("FirstName")

Code is so much easier than trying to explain a concept. :-)




回答2:


Does it have to be somewhat human readable? Or does the user not care... just save my settings... and let me reload them later?

If not, just create/design a bunch of objects with meaningful properties... create instances... and serialize (binary or soap or custom) the object to disk. Then, you can always "re-hydrate" your object later from disk.

When possible, I always like to not reinvent the wheel. Let the framework do the work.



来源:https://stackoverflow.com/questions/1583673/net-suggestions-on-making-a-config-file-for-a-program

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