C# 简单的非 xml 配置文件?

发布于 2024-08-09 07:14:22 字数 453 浏览 8 评论 0原文

C# 中是否有内置方法可以处理不使用 XML 的简单配置文件?或者也许有流行的免费图书馆?

我正在制作一个供非程序员使用的简单工具,并且我不想通过添加用于配置编辑的 UI 来使其复杂化。我只希望用户(对计算机足够熟悉)能够在记事本中打开配置文件并编辑/添加几行。这就是为什么我不想使用基于 XML 的配置文件。


编辑:感谢大家的回复!是的,绑定简单的 ui 来编辑基于 xml 的配置并不难。但它并不总是合适的。例如,对于非 GUI 应用程序,或者对于非常简单的应用程序,您不想添加任何更多 ui 以保持最大程度的简单。编写自己的配置解析器甚至不难,用于键/对设置或由空行分隔的块或设置。但我只是想知道有什么可用的,因为在 google 上快速搜索只会给我提供很多关于使用基于 xml 的配置的页面。

我不是全职软件开发人员,通常会编写一些小工具,使我和同事的工作生活变得更加轻松,因此使用已知且经过测试的解决方案自然会简化该任务。

Is there a build-in way in C# to work with simple config files which ate not XML? Or maybe there are popular free libraries?

I'm making a simple tool for non-programmers to use, and I don't want to complicate it by adding UI for configuration editing. I just want users (all comfortable enough with computers) to just be able to open config file in notepad and edit/add a few lines. That's why I don't want to use XML based config file.


EDIT: Thanks everyone for replies! Yes, it's not hard to bind simple ui for editing xml-based config. but it's not always suitable. for example for non-gui app, or for very simple app you don't want to add any more ui to keep it maximally simple. and it's not even hard to write own config parser, for key/pair settings or blocks or settings separated by a blank line. But I just wanted to know what's out there available, as quick search on google just gives me lots of pages on working with xml based config.

i'm not full time sw developer, and usually write small tools that make my and my colleagues' my life a little ezer at work, so using known and tested solutions, naturally, simplifies that task.

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

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

发布评论

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

评论(8

万人眼中万个我 2024-08-16 07:14:23

您可以使用 YAML,但是可用的 C# 库还相当不成熟。 YAML 是 JSON 的子集,您也可以使用它,它有多种可用的解析器,包括这个: http://www.codeplex.com/Json,它具有 LINQ to JSon 支持。

You could use YAML, however the C# libraries available are fairly immature. YAML is a sub-set of JSON, and you could just as well use that instead, it has a wide variety of parsers available, including this one: http://www.codeplex.com/Json, which has LINQ to JSon support.

半步萧音过轻尘 2024-08-16 07:14:23

我通常只是将配置绑定到网格控件或其他任何东西,然后让他们在应用程序内编辑配置。如果您将所有内容保留为基本的名称/值对,则不会涉及太多 UI。

I usually just bind the configuration to a grid control or whatever and let them edit the config inside the application. Not much UI involved if you keep everything as basically name/value pairs.

扭转时空 2024-08-16 07:14:23

Win32 API 有一些方便的函数,可以通过 P/Invoke 轻松调用:

Win32 API has a few handy functions that can be easily called via P/Invoke:

回首观望 2024-08-16 07:14:23

我真的不建议邀请/允许用户手动编辑配置文件,因为这可能会导致无法有效预测和预防的多种问题。我认为应对这种情况的最佳方法是创建一个配置文件编辑器,或者只是创建一个允许您保存设置的内置网格界面。这样,您就可以将其保存为任何格式,并避免不必要的麻烦。

至于以 INI 格式保存,您可以按照其他答案的建议使用 NiniKernel32 中本机 Windows INI 函数的包装。但是,如果您打算采用内置编辑器方法,则可以轻松地使用 XML。 .NET Framework 的内置 XML 函数使用起来非常简单。

现在,虽然我强烈认为允许用户手动编辑配置文件是一个禁忌,但在某些情况下,如果用户组很小并且每个人都将接受如何编辑文件的专门培训,那么这可能是更好的解决方案。如果您想采用这种方法,我推荐 INI 文件,因为它们很容易手动编辑、读取、解析和写入。您应该尝试两个 我在上面提到过这一点。

总之,我认为您确实应该考虑采用不同的方法,例如不允许用户手动编辑文件,而是提供内置编辑器,但是当然,如​​果您真的愿意,您可以采取手动编辑路线。

I don't really recommend inviting/allowing the user to edit the config files manually, as that can lead to SO many kinds of problems that just cannot be effectively anticipated and prevented. I think the best way to go with this situation is to create either an editor for config files or just a built-in grid interface that allows you to save settings. This way, you can save it in any format and avoid unnecessary hardships.

As for saving in an INI format, you can use, as other answers suggested, Nini or a wrapper for the native Windows INI functions in Kernel32. However, if you're going to take the built-in editor approach, you can easily use XML instead. .NET Framework's built-in XML functions are quite simple to use.

Now, while I strongly feel that allowing users to edit config files manually is a no-no, in some cases, it may be the preferable solution, if say the user group is small and everyone will be specifically trained how to edit the files. If you want to take this approach, I recommend INI files, as they're quite simple to edit manually, read and parse, and write. You should try the two libraries I mentioned above for this.

In summary, I think that you really should consider a different approach, as in not allowing the user to edit the files manually, and instead to provide a built-in editor, but of course, if you really want to, you can take the manual-editing route.

没︽人懂的悲伤 2024-08-16 07:14:22

我通常使用 INI 文件进行简单配置。有一篇很好的关于代码项目的文章,它提供了本机 Windows 的包装器Kernel32 中的 INI 函数。

I usually use INI files for simple configuration. There's a nice article on The Code Project that provides a wrapper for the native Windows INI functions in Kernel32.

把昨日还给我 2024-08-16 07:14:22

您可以尝试我编写的一个名为 DotNet.Config 的库。

它使用简单的基于文本的文件进行配置。

配置保存在“config.properties”文件中,如下所示:

size=12
dateTime=1/12/2014
name=Terry Tester
color=Blue

items.A=hello list
items.B=guten tag list
items.C=bonjour list

numbers.X=10
numbers.Y=20
numbers.Z=30

它包括一个方便的加载程序,可以根据命名约定将设置直接转换并应用到成员变量上(包括将设置组加载到通用列表中)。

或者您可以简单地通过调用来获取设置:

AppSettings.Retrieve["mySetting"]

您可以找到一个示例

您可以在此处获取副本:https://github.com/jknight/DotNet.Config

You can try a library I wrote called DotNet.Config.

It uses simple text-based files for configuration.

Configuration is kept in a 'config.properties' file that looks like this:

size=12
dateTime=1/12/2014
name=Terry Tester
color=Blue

items.A=hello list
items.B=guten tag list
items.C=bonjour list

numbers.X=10
numbers.Y=20
numbers.Z=30

It includes a handy loader that casts and applies settings directly onto member variables based on naming convention (including loading groups of settings into generic lists).

Or you can simply grab a setting with a call to:

AppSettings.Retrieve["mySetting"]

You can find an example here.

You can grab a copy here: https://github.com/jknight/DotNet.Config

寻找我们的幸福 2024-08-16 07:14:22

我喜欢 Nini,它简化了配置文件 XML 的使用,但您也可以将它用于 INI 文件。

http://nini.sourceforge.net/

I like Nini, which simplifies using XML for configuration files, but you can also use it for INI files.

http://nini.sourceforge.net/

别把无礼当个性 2024-08-16 07:14:22

我想我会选择花时间构建一个简单的编辑器,并使用户远离配置文件编辑业务。否则,您最终可能不得不编写大量代码来捕获用户可能通过弄乱配置文件而注入系统的所有可能错误。我认为,当您控制文件格式并可以利用限制输入的 UI 时,代码会更简单。

但是,根据用户的不同,您可以选择每行一个键-分隔符-值约定。读起来很简单。

我使用的第三方工具之一有一个非常简单的配置文件。它是一个简单的、自由格式的文本文件,其形式如下:

keyword [args]

它期望每一行都以关键字开头,该行之后的任何内容都将被简单地视为关键字的参数。它没有键/值对作为参数的概念 - 更像是列表。空格是该配置文件的分隔符。可能不适用于每种实现,但它适用于该实现。

绑定非常简单,从用户可以用作编辑器的绑定网格控件读取/写入 XML 文件,它肯定消除了简单的“在记事本中打开它”解决方案所产生的许多格式问题。

如果您的用户群非常精明但不是开发人员,那么纯文本解决方案也许就足够了。正如我的同事经常说的——没有必要在 2 美元的演出中投入 100 美元。显然,这取决于您的具体情况。

I think I'd opt to spend the time to build a simple editor and keep the user out of the config file editing business. Otherwise, you may end up having to write a lot of code to catch all the possible errors that the user could inject in the system by messing up the config file. When you control the file format and can take advantage of a UI that limits input, the code is simpler, I think.

But, depending on the users, you could just opt for the one-per-line key-separator-value convention. Reading that in is straightforward enough.

One of the third-party tools I use has a dirt-simple config file. It is a simple, free-form text file that has the form:

keyword [args]

It expects every line to start with a keyword and anything after that on the line is simply taken as arguments to the keyword. It doesn't have the notion of key/value pairs as args - more like just lists. Spaces are the delimiter for that config file. Might not work for every implementation, but it works for that one.

Binding is pretty straightforward and reading/writing an XML file from a bound grid control that the user can utilize as their editor, it certainly eliminates many of the formatting issues that arise from a simple "open it in notepad" solution.

If your user base is pretty savvy but just not developers, perhaps the plaintext solution would suffice. As my coworker says often - no need to put $100 in a $2 show. Obviously, it'll depend on your particular situation.

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