Not Rocket Science » A simple .ini File parser for C# (original) (raw)
Okay, so back in the old days, we had INI Files to store settings in. Those files are very simple:
[Section]
Key=Value
SomeOtherKey=SomeOtherValue[AnotherSection]
Hello=World
Hardly exciting, but it gets the job done very well. Today I wanted to work with INI Files in my C# Application, both for reading and writing. But as I just found out, .net does not have built in functions for working with .ini files. It looks like they want everyone to work with XML Files now. Well, apart from the fact that XML looks like my keyboard puked on my hard drive and that the format is unneccesarily complicated for this purpose, I also found that reading and writing them is rather complicated, especially since the XmlSerializer has some stupid restrictions (one being that the class needs to be public - have they never heard of reflection?).
It's great that the .net Framework is so powerful - I believe I can write a function that downloads an MP3 off the internet and uses it to generate a gradient in an image which then gets encrypted and sent per e-Mail in maybe 5 lines or so, but when it comes to something simple as saving some Key/Value Pairs, I still need a crapton of code. So I binged the internets a bit and found some code that uses P/Invoke to call Kernel functions to handle ini files, but I want to avoid P/Invoke at all costs. So in the end I simply wrote a class to Load/Save Ini Files.
It's a simple class to interact with a simple file format that was created to store Key/Value pairs and that just works. Seriously, don't change what isn't broken. The class is licensed under WTFPL and can be downloaded here.
August 15th, 2009 inDevelopment, My Tools | tags: rant