Tag Archives: 3.5

Cinchoo – Turn your app to Windows Tray app

In this article, I’ll show you how to turn your console / winform application to Windows System Tray application. Cinchoo framework provides a single hosting infrastructure to turn your application either Windows Service or Windows Tray application through configuration.

Console Application

Here is how you can do it for console application

1. Create a new ‘Console Application‘ from VS.NET

2. Add reference to Cinchoo.Core.dll

3. Add namespace Cinchoo.Core

4. Create a class derived from ChoApplicationHost as below

[RunInstaller(true)]
public class AppHost : ChoApplicationHost
{
    protected override void OnStart(string[] args)
    {
        //TODO: Application Startup code goes here
        base.OnStart(args);
    }
}

Decorating the above class with RunInstallerAttribute will make the application to be run as Windows Service. And override OnStart method, where application start up code placed there.

5. In the main entry, do as below.

public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.Run(new AppHost(), args);
    }
}

That’s all, you application is now ready to run as self installable windows service application or Windows Tray application.

Here is how to turn your application to Windows Tray application. In ChoCoreFrx.xml file, set ‘turnOn’ flag to ‘true’ in trayApplicationBehaviourSettings element.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <globalApplicationSettings applicationId="TestApplication.exe" eventLogSourceName="TestApplication.exe" turnOnConsoleOutput="false">
    <behaviourSettings hideWindow="false" bringWindowToTop="false" alwaysOnTop="false" runAtStartup="false" runOnceAtStartup="false" singleInstanceApp="false" activateFirstInstance="false" />
    <trayApplicationBehaviourSettings turnOn="true" showInTaskbar="true" hideMainWindowAtStartup="true" hideTrayIconWhenMainWindowShown="false" trayAppTurnOnMode="OnMinimize" />
    <appConfigPath />
  </globalApplicationSettings>
</configuration>

Other parameters

  • showInTaskbar – true, will show the application in Taskbar. false, otherwise.
  • hideMainWindowAtStartup – true, will hide the window at the application startup. Otherwise false.
  • hideTrayIconWhenMainWindowShown – true, tray application icon will hidden when the main windows shown. Otherwise false.
  • trayAppTurnOnMode – This option is applicable to WinForm application only. Possible options are OnMinimize, OnClose, OnMinimizeOrClose.

WinForm Application

Below are the steps to turn your winform application into Windows Tray application

1. Create a new ‘WinForm Application‘ from VS.NET

2. Add reference to Cinchoo.Core.dll

3. Add namespace Cinchoo.Core

4. Create a class derived from ChoApplicationHost and IChoWinFormApp as below

[RunInstaller(true)]
public class AppHost : ChoApplicationHost, IChoWinFormApp
{
    MainForm form = new MainForm();
    public AppHost()
    {
    }

    public Form MainFormWindow
    {
        get { return form; }
    }

    public ContextMenu GetContextMenu(ContextMenu contextMenu)
    {
        //Build the context menu items
        return contextMenu;
    }

    public string TooltipText
    {
        get { return null; }
    }

    public System.Drawing.Icon TrayIcon
    {
        get { return null; }
    }

    public string BalloonTipText
    {
        get { return null; }
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
    }
}

Decorating the above class with RunInstallerAttribute will make the application to be run as Windows Service. And override OnStart method, where application start up code placed there.

5. In the main entry, do as below.

public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.Run(new AppHost(), args);
    }
}

Thats all. Try it.


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

 


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 – 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);

Follow

Get every new post delivered to your Inbox.