Accessing via the Brackets [string]

Using the brackets is the easiest, but also the unsafest way. You can only get and string string values with the brackets.

Here is a list of all functions: Function Overview.

InIReader ini = new InIReader();

// Set value
ini["section key"]["entry key"] = "value";

// read value
string val = ini["section key"]["entry key"];

The Section

The first bracket select the InISection. It will either return the section, if it exists, create a new section or replace the existing section. You can append settings, using the .Add(InISection) method (Merge Files).

The Entry

Unlike the section selection, accessing the getter of the "entry" will throw an exception if the key does not exist. You can prevent the exception by using "TryGet" (Typesafe Interface) or by asking if the key does exist with "ContainsKey" which will return true key is accessible.

With the entry's setter, you can create or change the key's in the section.

InIReader ini = new InIReader(
"[section]\n" + 
"key1 = some text\n" +
"key2 = some text\n" );

ini["section"]["key1"] = "new text"; // changes key1's value
ini["section"]["xx"] = "new text"; // will create a new key
ini["xyx"]["key"] = "hello"; // will create the section xyx with the entry key

// but this will cause an exception, becaue key 3 does not exist
string val = ini["section"]["key3"];

Going safer

With Typesafe Interface, you won't have unwanted implicity typecasts and you don't have to do, all the ugly converting from and into a string.

Last edited Dec 12, 2012 at 5:31 PM by IqonX, version 3


No comments yet.