Tag Archives: INI

Cinchoo – All possible valid INI name values, Part 8

ChoIniDocument

This is the continuation of previous articles about handling INI section. In this section, I’m going to show you all the possible valid INI name values can be given in the INI document

[PRODUCT]
ENVIRONMENT                                                               ;No Value specified
VERSION = 1.002                                                           ;Valid Value specified
ADDRESS = 10 River Road, \
          Orlando, \
          FL 100230.                                                      ;Multi-Line value specified
CONNECTION_STRING1 = "PROVIDER=SQLServer;UserName=xxxx;Password=yyyyy"    ;Value with ';' characters
CONNECTION_STRING2 = "PROVIDER=Oracle;UserName=xxxx;
                      Password=yyyyy"                                     ;Multi-Line Value with ';' characters

 

Advertisements

Cinchoo – INI Parser Settings, Part 7

ChoIniDocument

Reading and writing INI files using Cinchoo framework can be controlled through global INI settings. These settings are available for your to edit at ChoIniSettings.xml file in your application binary folder. It will be created automatically when you run your application, if not exists. Here is the sample INI settings file looks like

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <iniSettings nameValueSeperator="=" commentChars=";#" ignoreValueWhiteSpaces="false" />
</configuration>

Where

  • nameValueSeperator – A character separate the key-value under section. Default value is ‘=’.
  • commentChars – List of INI comment character parser used to identify the comments. Default value is ‘;#’.
  • ignoreValueWhiteSpaces – true, to trim any white-spaces surrounded the value. Otherwise, preserve the white space with the value if any.

Cinchoo – Reading all INI sections, Part 7

ChoIniDocument

This is the continuation of previous articles about handling INI section. In this section, I’ll show you how you can read all the available INI section inside a INI document.. For a sample INI file below

;This is a test INI file.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMPANY=NAG Groups LLC
ADDRESS=10 River Road, \
        Orlando, \
        FL 100230.

[SOFTWARE]
OS1=Windows
ENVIRONMENT=PRODUCTION

Reading all the INI sections can be done as below

    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        foreach (ChoIniSectionNode iniSectionNode in iniDocument.Sections)
        {
            Console.WriteLine(String.Format("Key-Values for {0} Section...", iniSectionNode.Name));
            foreach (KeyValuePair<string, string> keyValue in iniSectionNode.KeyValues)
                    Console.WriteLine("{0} = {1}", keyValue.Key, keyValue.Value);
        }
    }

Cinchoo – Reading all INI section key values, Part 6

ChoIniDocument

This is the continuation of previous articles about handling INI section. In this section, I’ll show you how you can read all the key-values from a INI section. For a sample INI file below

;This is a test INI file.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMPANY=NAG Groups LLC
ADDRESS=10 River Road, \
        Orlando, \
        FL 100230.

Reading all key-values for PRODUCT section can be done as below

    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        ChoIniSectionNode productIniSectionNode;
        if (iniDocument.TryGetSection("PRODUCT", out productIniSectionNode))
        {
            foreach (string key in productIniSectionNode.Keys)
                Console.WriteLine("{0} = {1}", key, productIniSectionNode[key]);
        }
    }

OR

    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        ChoIniSectionNode productIniSectionNode;
        if (iniDocument.TryGetSection("PRODUCT", out productIniSectionNode))
        {
            foreach (KeyValuePair<string, string> keyValue in productIniSectionNode.KeyValues)
                Console.WriteLine("{0} = {1}", keyValue.Key, keyValue.Value);
        }
    }

Cinchoo – Multiline INI values, Part 5

ChoIniDocument

In this section, I’ll show you how you can store and read multiline values from Ini file. According to INI specification, multiline INI values are supported. In the below sample, ADDRESS section value is specified as multiline value. \ char is used to break the line in the unquote-ted value.

;This is a test INI file.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMPANY=NAG Groups LLC
ADDRESS=10 River Road, \
        Orlando, \
        FL 100230.

Alternatively you can give multi-line value in quotes as below

;This is a test INI file.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMPANY=NAG Groups LLC
ADDRESS="10 River Road,
        Orlando,
        FL 100230."

Try and see the value returned from the below statement…

using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
{
    Console.Writeline(iniDocument["PRODUCT"]["ADDRESS"]);
}

Cinchoo – Saving INI file, Part 3

ChoIniDocument

In this section, I’ll talk about on how to create INI document programmatically and save them. ChoIniDocument class provides abundant set of members helps you to create and add different types of   INI elements to the document.  Below are the different sections of INI document

  • ChoIniDocument – An INI document, contains heading ChoIniCommentNodes followed by series ChoIniSectionNodes, ChoIncludeIniSectionNodes, ChoIniCommentNodes or ChoIniNewLineNodes
  • ChoIniCommentNode – An INI comment node
  • ChoIniNewLineNode – An INI new line node
  • ChoIniSectionNode – An INI section node, may contain a Inline ChoIniCommentNode followed by series of ChoIniNewLineNodes, ChoIniCommentNodes, ChoIniNameValueNodes
  • ChoIniNameValueNode – An INI name value node
  • ChoIniIncludeFileNode – INI Include file node, it contains the same set of elements as ChoIniDocument contains.

Here I’ll show you on how to create a sample INI file programmatically

private static void CreateINIFile()
{
    ChoIniDocument.Clean(@"C:\Temp\TestIni1.ini");
    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        //Create INI document level comments
        iniDocument.AddHeadingComment("This is a test INI file.");
        iniDocument.AddHeadingComment("To test its functionality.");
        iniDocument.AppendNewLine();

        //Create and add PRODUCT section
        ChoIniSectionNode section = iniDocument.AddSection("PRODUCT");

        //Create and add VERSION name value to PRODUCT section
        ChoIniNameValueNode versionNode = section.AddNameValueNode("VERSION", "1.002");

        //Create and add COMPANY name value node to PRODUCT section
        ChoIniNameValueNode companyNode = section.AddNameValueNode("COMPANY", "NAG Groups LLC");

        //Add a newline node
        section.AppendNewLine();

        //Create and add INI include node
        ChoIniIncludeFileNode iniIncludeFileNode = iniDocument.AddIniIncludeFileNode(@"C:\Temp\TestIncludeIni1.ini");

        //Create and add SOFTWARE section to included node
        ChoIniSectionNode softwareSection = iniIncludeFileNode.AddSection("SOFTWARE");

        //Create and add OS1 name value node to SOFTWARE section
        softwareSection.AddNameValueNode("OS1", "MAC");

        //Create and add OS2 name value node to SOFTWARE section
        softwareSection.AddNameValueNode("OS2", "Windows7");

        Console.WriteLine(iniDocument.ToString());

        iniDocument.Save();
    }
}

It creates the below two INI files as below

C:\Temp\TestIni1.ini

;This is a test INI file.
;To test its functionality.

[PRODUCT]
VERSION=1.002 ;Version Comment
COMAPNY=NAG Groups LLC ;Company node

[INCLUDE("C:\Temp\TestIncludeIni1.ini")]

C:\Temp\TestIncludeIni1.ini

[SOFTWARE]
OS1=MAC
OS2=Windows7

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)