← Back to articles

.Net tools to use: Nini

.NET tools to use, part two: Nini, an uncommonly powerful configuration library. Reads web.config, .ini and .xml config with type conversion and default values in a couple of lines.

Tedd

** ** http://nini.sourceforge.net/

Nini

Â

Description

An uncommonly powerful .NET configuration library. Use this for reading web.config (instead of .Net built-in System.Configuration…) and any other .ini-file or .xml-file containing configuration.

Difficulty

2 lines to read (usually called at page_load or similar).

Sample usage:

[sourcecode language=“csharp”] // Set up our config source as web.config or app.config internal static IConfigSource ConfigRoot = new Nini.Config.DotNetConfigSource(DotNetConfigSource.GetFullConfigPath()); // Create a config object for section “appSettings” internal static Nini.Config.IConfig Config = cs.Configs[“appSettings”];

// Read some keys from appSettings section string mystring = Config.GetString(“SomeKey”, “DefaultValue”); int myint = Config.GetInt(“SomeKey2”, 10); int mybool = Config.GetInt(“SomeKey3”, true); [/sourcecode]

Advantages

\t

  • Automatic type conversion (GetString, GetInt, GetBool, etc).

\t

  • Configuration can be both read and written to.

\t

  • Can read/write to web.config, .ini-files, .xml-files (with hierarcy) and SQL-database.

\t

  • Changing configuration source is easily done by changing a single line. For example moving config from web.config to SQL at a later point in the project is just chaging a single line.

\t

  • Several files can be read and merged into the same instance, so you can split config into many files. So you can read parts from web.config and parts from SQL.

\t

  • This means that configuration can be moved from web.config to a SQL-table at any point with almost no effort. Storing config in SQL allows you to keep the files the same configuration between prod and test.

Written by Tedd · Oslo, Norway

Updated 9 Jul 2010

Follow the journal via RSS