开发者

c# simple non-xml config files?

开发者 https://www.devze.com 2022-12-09 18:11 出处:网络
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?

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.


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.


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


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

http://nini.sourceforge.net/


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.


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.


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.


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

  • GetPrivateProfileString
  • GetPrivateProfileInt
  • WritePrivateProfileString


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消