views:

521

answers:

7

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.

+1  A: 

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

http://nini.sourceforge.net/

quip
INI files are pretty simple, but if "joe-user" is editing them, no guarantee that they are formatted correctly. Wouldn't you be better of providing your own editor that writes to an XML or INI or WHATEVER file? How dynamic are the configuration settings?
quip
I agree; the only configurations Joe the user should be making would be best integrated within the programs ui somehow...
Gurdas Nijor
+3  A: 

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.

David Brown
A: 

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.

Mark Allen
A: 

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

Pavel Minaev
+1  A: 

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.

itsmatt
<quote>no need to put $100 in a $2 show</quote> -- exactly the situation. even though i might still write simple ui for editing and use xml, i was thinking along those lines.
flamey
A: 

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.

Maxim Zaslavsky
+1  A: 

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.

sgrassie