Sometimes we, developers, tend to have an itch. When i saw that there was a new kid in town (AppVeyor) that does free open source continuous integration, i just had to scratch.
As i resend xml configuration files, i love the simplicity of yaml and the older ini file formats.
But if you would like to read ini files in .net you have to do a dll import:
So i thought of creating a simple relaxed ini parsing library called ZenIni which you can obtain through Nuget.
Everything starts with constructing the provider, which you could register as a static instance in your DI container of choice:
The DefaultSettingsReader allows you to specify how strings should be compared so you can choose whether or not to ignore case.
The IniSettings class is the in memory representation of your ini file, which consists out of sections with or without nested key/value pairs.
Getting a section is as easy as
Even if the original file did not contain the section if will never return null, it will return the static instance Section.Empty. This to relieve you from checking for null when you need to access a value.
Given the following ini file
To get the status you just have to do:
There are some extension methods to help you with common used types like booleans and integer values. Given the same ini file, to get the age setting you call:
It returns a nullable, so if your ini file does not contain the setting you can just add a default like this:
Just give it a spin, it’s small and very easy. The specification is documented on the wiki.