Cinchoo – Reading INI file, Part 1

ChoIniDocument

Initialization files known as INI files provide a standard means for storing configuration information for software in a text file. Although rarely used by .NET applications, there are situations where these files must be read and written to using C#.

Older versions of Microsoft Windows used INI files to store configuration information for the operating system and its applications. These are simple text files that can be edited using software such as Notepad. They hold a series of key / value pairs organised into categories where all of the category names, key names and values are held as strings.

Here is the sample INI file

[COLORS]
Background=Black
Foreground=White

[FONTS]
Face=Arial
Size=12pt

In this section, I’ll talk about one of the class used to read and write INI document, aka ChoIniDocument. This class represents an INI document. It is an in-memory (cache) tree representation of an INI document and enables the navigation and editing of this document. Members of this class is NOT thread-safe.

Lets see how we can load a INI document.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core.Ini

static void Main(string[] args)
{
    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        Console.WriteLine(iniDocument.ToString());
    }
}

Below are the different overloads of ChoIniDocument can be used creates a new INI document from a file specified by a URI, from an TextReader, or from text.

public static ChoIniDocument Load(Stream stream, ChoIniLoadOptions loadOptions);
public static ChoIniDocument Load(TextReader reader, ChoIniLoadOptions loadOptions);
public static ChoIniDocument Load(string path, ChoIniLoadOptions loadOptions);
public static ChoIniDocument Parse(string text, ChoIniLoadOptions loadOptions);

Where ChoIniLoadOptions is the options used by the loading process, contains the below numbers

  • NameValueSeperator (char) – Name Value Seperator (default, ‘=’)
  • CommentChars (string) – Possible Comment Characters (default, “#;”)
  • IgnoreValueWhiteSpaces (bool) – true, Ignore White Spaces (default, false)

		
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s