(.Net) 关于为程序制作配置文件的建议? [英] (.Net) suggestions on making a config file for a program?

查看:36
本文介绍了(.Net) 关于为程序制作配置文件的建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不一定指的是 app.configs,而是一个自定义配置文件,它会在用户点击保存"按钮时存储我的程序状态.

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.

在我的示例中,它是一个 UI 构建器,允许用户选择要在两列屏幕的左右列上显示哪些字段",以及每列中字段的顺序.还有其他的东西,但你明白了.

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.

用户可以打开他们保存的任何配置文件并适当地填充 UI 构建器.

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

我认为这可以存储在 XML 文件中,但我想知道人们对如何写出它、如何从中读取(可能使用 LINQ?)和其他一般方法有什么样的建议,包括版本控制等.我相信这之前已经出现过,我想听听最佳实践.谢谢!

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!

推荐答案

我用于我的程序的方法是将我的配置值保存在一个类中,该类使用 DataSet 中的 DataTable 来存储值.当我想保存它时,我使用 DataSet.WriteXML() 到用户想要的任何文件名.

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.

我目前的使用非常简单(一个带有键、值列的数据表),但它很容易扩展到多个相关的数据表,其中包含用于各种信息的特定列.DataSet 处理文件 io.

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.

然后使用 DataSet.ReadXML() 回读是轻而易举的.

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.

这是一个简单的示例类.

像这样使用,制作一个这样的文件:

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. :-)

这篇关于(.Net) 关于为程序制作配置文件的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆