在像“config.cfg”这样的文本文件中存储程序设置的逻辑是怎样的?

发布于 2024-11-05 18:10:37 字数 515 浏览 1 评论 0原文

我经常看到一些软件,特别是游戏,将设置存储在文本文件中。我也想在我的 C# WinForms 应用程序中拥有同样的东西(我知道可以对用户和软件端的应用程序设置进行操作),因为我认为最终用户更容易处理它。

所以我需要知道的是如何读取这些设置文件,例如设置类的字段。

想象我有一个具有以下字段的类:

private double vOffset = 0;
private bool Refresh = false;

这将是我的文本文件(假设文件名为“config.cfg”):

;This is a comment
Voltage Offset = 0.08
;Refresh Enable = 1 | Disable = 0
Refresh = 1

想听听您的意见想法,也许还有一些代码!既用于读取此文件和更新类字段,也用于如何从软件内保存或更新此列表。

谢谢。

Often I see some software and specially games that store setting in a text file. I want to have same thing in my C# WinForms application too (I know it is possible to do with application settings for both user and software side) Since I think it is easier for end user to deal with it.

So what I need to know is how these settings files are being read and for example set the fields of a class.

Imagine I have a class that has the following fields:

private double vOffset = 0;
private bool Refresh = false;

And here will be my text file (lets say file is named "config.cfg"):

;This is a comment
Voltage Offset = 0.08
;Refresh Enable = 1 | Disable = 0
Refresh = 1

Would like to hear your ideas and propably some codes maybe! both for reading this file and updating class fields and also how to save or update this list from within the software.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

居里长安 2024-11-12 18:10:37

您可以使用标准 .NET 设置类吗?

优点:
- 你不需要为此编写代码
- 您可以通过 UI 来编辑默认设置
- 您以键入的方式引用代码中的设置,例如 Settings.Default.Test1
- 您的设置将存储在 XML 格式的应用程序配置文件中。

此处有更多详细信息:http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx

Settings

在配置文件中,它看起来像这样:

  <setting name="Test2"
           serializeAs="String">
    <value>My string setting</value>
  </setting>

您可能会认为 XML 对于最终用户来说不容易阅读。但我相信,了解“电压偏移”是什么的用户在读取/更新 xml 文件中的设置时应该不会遇到问题:)。

Can you use standard .NET settings classes?

Advantages:
- you don't need to code for this
- you have UI to edit your default settings
- you reference settings in the code in a typed manner, like this Settings.Default.Test1.
- your settings will be stored in the application config file, which is in XML.

More details here: http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx.

Settings

In config file it would look like this:

  <setting name="Test2"
           serializeAs="String">
    <value>My string setting</value>
  </setting>

You might argue that XML is not easy to read for the end user. But I believe that users who understand what 'Voltage Offset' is, should not have issues reading/updating settings in xml file :).

罗罗贝儿 2024-11-12 18:10:37

实际上,在 .net 环境中执行此操作的最简单方法是使用 xml 设置文件。使用 xmlserializer,您可以将对象序列化为文本并从文本反序列化。

http://msdn.microsoft.com/en-us /library/system.xml.serialization.xmlserializer.aspx

编辑:xmlserializer 也不会序列化私有字段/属性,因此您应该使用公共属性来获得所需的结果。

Actually the easiest way for you to do it in a .net environment is to have xml settings file. Using an xmlserializer you can serialize an object to text and deserealize from text.

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Edit: also xmlserializer won't serialize private fields/properties, so you should use public properties to get the desired result.

も星光 2024-11-12 18:10:37

设置通常以一种常见格式存储
例如 XML(现在很常见)、INI(曾经很常见)、LAU……

将对象序列化为这些格式或从它们反序列化
可以使用内置序列化器(例如.NET 中的 XmlSerializer)、第 3 方序列化器来完成
甚至是适当的定制的。

The setting are usually stored in one of the common formats
E.g. XML (very common now), INI (used to be common), LAU, ...

Serializing object to these formats or deserializing from them
can be done with built in serializers (e.g. XmlSerializer in .NET), 3rd party ones
or even propriety custom ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文