Tag Archives: VB.NET

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 – Accessing INI sections, Part 4

ChoIniDocument

In this section, I’ll walk you over accessing different parts of INI document using ChoIniDocument class

Using Indexer

First and easy way, you can use the indexer to access the key-value information if you have key name in hand. This will search for key-value in the nested INI files as well.

For a sample INI files 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

Code below shows how to access name-values using indexer

private static void LookupNameValues()
{
    using (ChoIniDocument iniDocument = ChoIniDocument.Load(@"C:\Temp\TestIni1.ini"))
    {
        //Lookup PRODUCT/VERSION
        Console.Writeline(iniDocument["PRODUCT"]["VERSION"]);

        //Lookup SOFTWARE/OS1 in nested INI file
        Console.Writeline(iniDocument["SOFTWARE"]["OS1"]);
    }
}

Using Section Access Methods

You can access INI sections using one of the below overload methods

public ChoIniSectionNode GetSection(string sectionName);
public bool TryGetSection(string sectionName, out ChoIniSectionNode section);

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 Nested INI files, Part 2

ChoIniDocument

In this section, I’ll talk about one of the most important feature Cinchoo framework provides in reading INI file is that the support of  Nested INI files. It gives the flexibility of breaking down large INI file into multiple small and manageable INI files. Here is how you can do it using Cinchoo framework,

Nested INI files can be setup as below

Main.ini file

;This is a test INI file.

[PRODUCT]
VERSION=1.002
COMAPNY=NAG Groups LLC

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

[ENVIRONMENT]
VERSION=1.0.0.1
PATH=C:\WINDOWS

In main.ini file, we included TestInclude.ini file using [INCLUDE] tag. Tag is case-sensitive. Also the path can specified as absolute/relative path.

TestInclude.ini file

[SOFTWARE]
OS1=MAC
OS2=Windows7

Loading Main.ini file using ChoIniDocument.Load() will load all the included INI files and build the tree in memory seamlessly.

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

PS: Make sure all the main as well as included INI files contains distinct INI sections (no duplicate section allowed). Otherwise load will fails with exception.


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)

				

Cinchoo – Abortable Queued Execution Service

ChoAbortableQueuedExecutionService

In this article, we will go over the one of service provided by Cinchoo framework, ChoAbortableQueuedExecutionService. This service used to execute series of methods in orderly fashion. Each operation pushed to this service will be executed in first come first serve basis and those are abortable anytime. Also we can instruct to run each operation with Timeout (ms), Number of retries (in case of error while running), sleep between retries parameters.

Either you can use Global AbortableQueuedExectionService (application level service) or instantiate new one locally and use them.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

Using Global AbortableQueuedExecutionService:

Below sample, inject a delegate to Global abortable execution service and wait for the call to complete.

static void Main(string[] args)
{
    IChoAbortableAsyncResult result = ChoQueuedExecutionService.Global.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Creating and using local AbortableQueuedExecutionService:

In below sample, we create a local AbortableQueuedExecutionService and injecting method to execute.

static void Main(string[] args)
{
    ChoQueuedExecutionService service = new ChoQueuedExecutionService("TestQService", false);

    IChoAbortableAsyncResult result = service.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Aborting Operation:

Any operation queued to this service can be aborted anytime. In here, I’ll show you how we can abort a operation after queue the request to the service.

static void Main(string[] args)
{
    ChoQueuedExecutionService service = new ChoQueuedExecutionService("TestQService", false);

    IChoAbortableAsyncResult result = service.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    Thread.Sleep(1000);
    result.Abort(); //Abort the operation
}

Try for yourself!


Cinchoo – Queued Execution Service

ChoQueuedExecutionService

In this article, we will go over the one of service provided by Cinchoo framework, ChoQueuedExecutionService. This service used to execute series of methods in orderly fashion. Each operation pushed to this service will be executed in first come first serve basis. Also we can instruct to run each operation with Timeout (ms), Number of retries (in case of error while running), sleep between retries parameters.

Either you can use Global QueuedExectionService (application level service) or instantiate new one locally and use them.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

Using Global QueuedExecutionService:

Below sample, inject a delegate to Global execution service and wait for the call to complete.

static void Main(string[] args)
{
    IChoAsyncResult result = ChoQueuedExecutionService.Global.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Creating and using local QueuedExecutionService:

In below sample, we create a local QueuedExecutionService and injecting method to execute.

static void Main(string[] args)
{
    ChoQueuedExecutionService service = new ChoQueuedExecutionService("TestQService", false);

    IChoAsyncResult result = service.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Try for yourself!


Cinchoo – Delegate.WaitFor(), Extension Method

Here I’ll talk about WaitFor extension method. This extension method used to run a method with finite number of retries if it failed to run at first time and/or in a finite time frame. It provides most of Action and Func delgates overloads.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

Sample 1:

Below are the list of overloads for Func<T> delegate, Cinchoo provides these overloads almost all the Func’s and Action’s.

public static TResult WaitFor(this Func func);
public static TResult WaitFor(this Func func, int timeout);
public static TResult WaitFor(this Func func, int timeout, int maxNoOfRetry);
public static TResult WaitFor(this Func func, int timeout, int maxNoOfRetry, int sleepBetweenRetry);

Below sample shows you how to run a  Func<T> delegate method using WaitFor() extension method.

class Program
{
    static void Main(string[] args)
    {
		Func f1 = () =>
			{
				System.Threading.Thread.Sleep(1);
                Console.WriteLine("Running method...");
				return 1;
			};

        Console.WriteLine("Output: {0}".FormatString(f1.WaitFor()));
	}
}

When you run the above code, the output will be

Running method...
Output: 1
Press any key to continue . . .

Sample 2:

In this example, I’ll show you the way of invoking a method with timeout (1000ms) parameter. It throws a timeout exception, as the method takes more time to execute than timeout (1000 ms) period.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Func f1 = () =>
                {
                    System.Threading.Thread.Sleep(5000);
                    Console.WriteLine("Running method...");
                    return 1;
                };

            Console.WriteLine("Output: {0}".FormatString(f1.WaitFor(1000)));
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}".FormatString(ex.Message));
        }
	}
}

When you run the above code, the output will be

Error: Timeout [1000 ms] elapsed prior to completion of the method [Target: , Metho
d: Int32 b__0()].
Press any key to continue . . .

Sample 3:

In here, we will see how a method with an error is executed for number of times with one of the WaitFor overload.

static void Main(string[] args)
{
    try
    {
        Func<int> f1 = () =>
            {
                System.Threading.Thread.Sleep(10);
                throw new ApplicationException("Communication error.");
            };

        Console.WriteLine("Output: {0}".FormatString(f1.WaitFor(Timeout.Infinite, 3)));
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: {0}".FormatString(ex.Message));
    }
}

When you run the above code, the output will be

Error: Exception(s) occurred : The method failed to execute after 3 retries..
[ System.ApplicationException: Communication error.
   at Cinchoo.Core.ChoWaitForTest.Program.<Main>b__0() in C:\Personal\Cinchoo.Fr
amwork\Cinchoo.Core.Test\Cinchoo.Core.ChoWaitFor.Test\Program.cs:line 18
   at Cinchoo.Core.ChoWaitFor.WaitFor[TResult](Func`1 func, Int32 timeout, Int32
 maxNoOfRetry, Int32 sleepBetweenRetry) in C:\Personal\Cinchoo.Framwork\Cinchoo.
Framework\Cinchoo.Core\WaitFor\ChoWaitFor.cs:line 148 ]

[ System.ApplicationException: Communication error.
   at Cinchoo.Core.ChoWaitForTest.Program.<Main>b__0() in C:\Personal\Cinchoo.Fr
amwork\Cinchoo.Core.Test\Cinchoo.Core.ChoWaitFor.Test\Program.cs:line 18
   at Cinchoo.Core.ChoWaitFor.WaitFor[TResult](Func`1 func, Int32 timeout, Int32
 maxNoOfRetry, Int32 sleepBetweenRetry) in C:\Personal\Cinchoo.Framwork\Cinchoo.
Framework\Cinchoo.Core\WaitFor\ChoWaitFor.cs:line 148 ]

[ System.ApplicationException: Communication error.
   at Cinchoo.Core.ChoWaitForTest.Program.<Main>b__0() in C:\Personal\Cinchoo.Fr
amwork\Cinchoo.Core.Test\Cinchoo.Core.ChoWaitFor.Test\Program.cs:line 18
   at Cinchoo.Core.ChoWaitFor.WaitFor[TResult](Func`1 func, Int32 timeout, Int32
 maxNoOfRetry, Int32 sleepBetweenRetry) in C:\Personal\Cinchoo.Framwork\Cinchoo.
Framework\Cinchoo.Core\WaitFor\ChoWaitFor.cs:line 148 ]
Press any key to continue . . .

Happy coding!!!


Cinchoo – Collections, NestedList, Part 7

GetContainedList

ChoNestedList<T> provides a helper method to find the list containing an item.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core.Collections.Generic

Sample code,

static void Main(string[] args)
{
    ChoNestedList topList = new ChoNestedList();
    topList.Add("TopList-Tom");
    topList.Add("TopList-Mark");

    ChoNestedList nestedList1 = new ChoNestedList();
    nestedList1.Add("NestedList1-Raj");
    nestedList1.Add("NestedList1-Peter");
    nestedList1.Add("NestedList1-Samuel");
    topList.Add(nestedList1);

    topList.Add("TopList-Nancy");

    foreach (string name in topList.GetContainedList("NestedList1-Raj"))
        Console.WriteLine(name);
}

When you run the above code, the output will be

NestedList1-Raj
NestedList1-Peter
NestedList1-Samuel
Press any key to continue . . .

Follow

Get every new post delivered to your Inbox.