在哪里存储配置信息 [英] Where to store configuration information

查看:199
本文介绍了在哪里存储配置信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,我从C重建到C#。此应用程序必须能够支持存储信息的传统方法,例如来自命令行的参数和来自定制每个运行的文件(称为系统参数)的参数。系统参数文件是纯文本,带有一个简单的键:值结构。

I have a console application that I am rebuilding from C to C#. This application has to be able to support the legacy method of storing information like parameters from a command-line and parameters from a file (called the system parameters) that customize each run. The system parameters file is in plain-text with a simple key : value structure.


  • 我应该将这些不同的参数合并到单个配置对象中吗?

  • 如何从代码中调用此配置对象来存储参数?

  • 如何从代码中调用此配置对象以检索参数?


    • 这个物件是否为强类型?

    我有一个感觉,它应该是一个单一的强类型对象,它应该是一个实例化的对象,从存储库中检索与静态检索方法,但我真的想验证此方法。

    I have a feeling that it should be a single, strongly-typed object and that it should be an instantiated object that is retrieved from a repository with a static retrieval method however I really want validation of this method.

    提前感谢

    推荐答案

    a href =http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.aspx =nofollow noreferrer> 设置 。这些可以通过使用添加新文件对话框创建设置文件或通过从项目属性添加默认设置文件自动生成。每个设置可以在用户或应用程序范围中,它们控制用户是否可以更改它们或将其限制为其默认值。它们可以使用 Save()方法轻松保存,并自动加载到静态的默认属性中。

    I like using Settings. These can be generated automatically either by creating a Settings File using the Add New File dialog box, or by adding a default Settings File from Project Properties. Each setting may be in User or Application scope, which controls whether or not the user can change them or they are restricted to their default values. They are easily saved with the Save() method and loaded automatically into the static Default property.


    这个类似乎是用于应用程序或基于用户的设置。我在寻找每次运行设置。在这种情况下,你仍然建议使用这个类吗? - x97mdr

    This class seems to be for application or user-based settings. I'm looking for per-run settings. Would you still recommend using this class in that case? – x97mdr

    是的。如果您同时具有基于用户/应用程序的设置和每次运行设置,则应该使用两个不同的类 - 正常(保存)设置和每次运行设置。只要你不保存每次运行的设置,你应该是安全的,设置仍然很容易使用。这些是静态设置。如果相同的应用程序运行需要几个实例 - 这是错误的方法。

    Yes. If you have both user/application based settings and per-run settings you should use two different classes - the normal (saved) settings and the per-run settings. As long as you don't save the per-run settings, you should be safe and settings are still quite easy to use. These are static settings though. If the same application run needs several instances - this is the wrong approach.

    这篇关于在哪里存储配置信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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