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

查看:190
本文介绍了(.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 确定这之前已经出现,我想听听最佳做法。谢谢!

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.

我目前使用很简单(一个DataTable键,值列),但它可以轻松扩展到多个相关的DataTable,并提供各种信息的特定列。 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.

以下是一个简单的示例类

像这样使用这样的文件

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天全站免登陆