在作出配置文件的程序(.NET)的建议? [英] (.Net) suggestions on making a config file for a program?

查看:110
本文介绍了在作出配置文件的程序(.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.

在我的例子,这是一个用户界面生成器,允许用户选择哪个田是在一个两列屏幕上显示的左,右列,沿着与各列中的字段的顺序。还有其他的东西太多,但你的想法。

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!

推荐答案

我用我的程序的方法是保持我的配置值在使用一个DataTable DataSet中的存储值的一类。当我要救它,我用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.

我目前使用的是pretty的简单(一个数据表与关键,值列),但它很容易地扩展到多个相关数据表与各种信息的具体列。该数据集处理文件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().

此方法在序列化类的一个优点是,它更容易处理保存从你的程序的previous版本的文件。您可以在表中值的程序用来保存它,所以你的新版本就知道了旧文件将不会在新的版本中增加任何价值的版本 - 它可以适当地将其设置为更新文件版本。

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是因此比试图解释一个概念要容易得多。 : - )

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

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

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