Tag Archives: VB.NET

Cinchoo – Bulk Files & Folders rename utility

ChoBulkFileRenameTool

Download Binary (Zip)

Download Source (Zip)

This tool helps to rename bulk files and folders recursively. It was written in using WPF/C#/VS.NET 2010.

Features

  • Bulk file and folders rename matching either wildcard or regular expression patterns
  • Runs on Windows 95/98/ME/NT4/2k/2k3/2k8/XP/Vista/7

How to use

  • Click ‘Start’ whenever you are ready to rename files. Any time you can can start this process.
  • Type a full path of the file or folder in ‘Choose a File / Folder’ text box, then click ‘Add’ button to add it to ‘File(s) / Folder(s)’ list box. Or you can click ‘Browse’ button, to choose the file/folder for rename.
  • At any time, you can select an item in ‘File(s) / Folder(s)’ list box, remove it using ‘Remove’ button.
  • In ‘Find/Replace’ text boxes, you can specify either the wildcard or regular expression to identify files and folders for rename. (Please visit Regular Expression Cheat Sheet for help in creating regular expression)
  • Check ‘Match Files’ or ‘Match Folders’ check boxes, in order to include them for rename process respectively.
  • Check ‘Preview Mode’ option, if you just want to see the files and folders are renamed in the ‘Status’ box or in the log file under ‘Logs’ folder. This option will not rename the files or folder names.
  • Check ‘Include Sub Dir’ option, to include all sub directories for rename process.
  • In ‘Status’ box, you can view the statues of the file renames.
  • Click ‘Clear’ button anytime to clear the ‘Status’ box.

Tips

  • You can drag and drop file(s) or folder(s) to this tool for easy cleanup process.

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

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

Decorating the above class with ChoApplicationHostAttribute 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(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

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
    MainForm form = new MainForm();
    public AppHost()
    {
    }
    public Form MainFormWindow
    {
        get { return form; }
    }

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

Decorating the above class with ChoApplicationHostAttribute 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(args);
    }
}

Thats all. Try it.

Cinchoo – Configuration framework, part 24

Binding to WinForm controls

Download Source Files (ZIP)

In this section, I’ll talk about binding your configuration object to WinForm controls. WinForm has rich binding infrastructure, Cinchoo framework works closely with WinForms to bind the configuration object to them seamlessly. Lets walk over how you can do this.

For a sample configuration object below

[ChoNameValueConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
    [ChoPropertyInfo("name", DefaultValue = "Mark")]
    public string Name
    {
        get;
        set;
    }

    [ChoPropertyInfo("message", DefaultValue = "Hello World!")]
    public string Message
    {
        get;
        set;
    }
}

PS: WinForm controls only binds to public Properties only. Please make sure you define them accordingly in your configuration object.

You can bind the above configuration object to your WinForm window in either constructor or window load event handler as below.

private void MainForm_Load(object sender, EventArgs e)
{
    SampleConfigSection settings = new SampleConfigSection();
    textBox1.DataBindings.Add("Text", settings, "Name");
    textBox2.DataBindings.Add("Text", settings, "Message");
}

That’s all. Now the changes made to configuration source will be reflected in the controls as well as the changes made to controls will be persisted automatically to underlying source based on the binding nature. Try for yourself.

Cinchoo – Configuration framework, part 23

Passing Configuration FilePath Programmatically

UPDATED: In this topic, I’m going to show you how to pass configuration object filepath programmatically. Cinchoo framework addresses this feature through IChoConfigurationParametersOverridable interface. When you define a configuration object, implement this interface as below to override the configuration file path.

[ChoNameValueConfigurationSection("applicationSettings")]
public class ApplicationSettings : ChoConfigurableObject
{
    [ChoPropertyInfo("path", DefaultValue = @"C:\")]
    public string Path;

    [ChoPropertyInfo("OS", DefaultValue = "Windows")]
    public string OS;

    protected override void OverrideMetaDataInfo(ChoBaseConfigurationMetaDataInfo metaDataInfo)
    {
        ChoStandardConfigurationMetaDataInfo info = metaDataInfo as ChoStandardConfigurationMetaDataInfo;
        info.ConfigFilePath = @"C:\Test\ApplicationSettings.xml";
    }
}

Cinchoo – Configuration framework, part 22

Binding to WPF Controls

Download Source Files (ZIP)

In this section, I’ll talk about binding your configuration object to WPF controls. As WPF provides rich binding infrastructure, Cinchoo framework take a step closer to bind the configuration object to WPF controls seamlessly. Lets walk over how you can achieve this.

For a sample configuration object below

[ChoNameValueConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
	[ChoPropertyInfo("name", DefaultValue="Mark")]
	public string Name;

	[ChoPropertyInfo("message", DefaultValue="Hello World!")]
	public string Message;
}

You can bind the configuration object to your WPF window in either constructor or window loaded event handler as below.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ChoWPFBindableConfigObject<SampleConfigSection> bindObj = new ChoWPFBindableConfigObject<SampleConfigSection>();
    this.DataContext = bindObj;
}

That’s it. Now you can bind each control to this context object members. For example, the below snippet binds a text box to ‘Name’ property of above configuration object.

<TextBox Name="txtValidExts" Text="{Binding Path=Name}" />

Now the changes made to configuration source will be reflected in the controls as well as the changes made to controls will be persisted automatically to underlying source based on the binding nature. Try for yourself.

Cinchoo – Configuration framework, part 21

Configuration Storage Plug-in

This article is the continuation of Cinchoo configuration framework series. So far you learned and used various configuration section handler provided by Cinchoo framework in your applications. But it limits the accessibility of the underlying configuration data source somehow. Now Cinchoo framework opens the possiblity of extending the configuration framework by creating your own plug-ins to access various configuration sources by using IChoDictionaryConfigObjectAdapter interface.

Let say, you have configuration data available in Database, FTP, HTTP, WebService, WCF, email etc. Now those can be accessed easily using Cinchooframework. Here I’m going to walk you over in implementing one such configuration data source reside in database using ADO.NET interface. (You can try Linq to Sql, ADO.NET Entity Framework etc as well).

IChoDictionaryConfigObjectAdapter interface exploses the below members

public interface IChoDictionaryConfigObjectAdapter
{
    IDictionary GetData();
    void Save(IDictionary dict);
    DateTime LastUpdateDateTime
    {
        get;
    }
}

Here are the steps to create a new adapter class

  1. Create a new class (say, ChoMySqlADODictionaryConfigObjectAdapter) derived from IChoDictionaryConfigObjectAdapter interface. Implement the interface members.
  2. GetData() member will be called by framework to retrieve the configuration data. In here you establish connection to underlying source, retrieve the data and return them in IDictionary format.
  3. Save() method will be called by framework to save the snapshot (in IDictionary format) of configuration data back to underlying source. Here you establish the connection to the underlying source, save them. Important, while saving, make sure you update the modified timestamp with current timestamp.
  4. LastUpdateDateTime property will be invoked periodically by framework to identify the changes to the underlying configuration source. In here, you will have to establish the connection to the source and retrieve the modified datatime.
public class ChoMySqlADODictionaryConfigObjectAdapter : IChoDictionaryConfigObjectAdapter
{
    public IDictionary GetData()
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();

        //Connect to the source, load the configuration object payload

        return dict;
    }

    public void Save(System.Collections.IDictionary dict)
    {
        if (dict != null && dict.Count > 0)
        {
            //Connect to the source, save the data
        }
    }

    public DateTime LastUpdateDateTime
    {
        get
        {
            //Connect to the source, get the last updated timestamp
            return DateTime.MinValue;
        }
    }
}

Now here is how to use the above adapter in your application

Define a configuration class ‘ApplicationSettings’, decorated with ChoDictionaryAdapterConfigurationSection attribute as below with sectionName, and the type of the adapter ChoMySqlADODictionaryConfigObjectAdapter. That’s all. Your configuration object is all set to use the configuration object data from database using ChoMySqlADODictionaryConfigObjectAdapter.

[ChoDictionaryAdapterConfigurationSection("mySqlDictionarySectionHandlerTest/applicationSettings", typeof(ChoMySqlADODictionaryConfigObjectAdapter))]
public class ApplicationSettings : ChoConfigurableObject
{
    #region Instance Data Members (Public)

    [ChoPropertyInfo("path", DefaultValue = @"C:\")]
    public string Path;

    [ChoPropertyInfo("OS", DefaultValue = "Windows")]
    public string OS;

    [ChoPropertyInfo("singleInstanceApp", DefaultValue = false)]
    public bool SingleInstanceApp;

    #endregion
}

Here is how you can instantiate and use the above configuration object

static void Main(string[] args)
{
    ApplicationSettings applicationSettings = new ApplicationSettings();
    Console.WriteLine(applicationSettings.ToString());

    ChoFramework.Shutdown();
}

The corresponding configuration section will be created automatically in [appExeName].xml file under bin/Config folder

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <mySqlDictionarySectionHandlerTest>
    <applicationSettings cinchoo:configObjectAdapterType="Cinchoo.Core.Configuration.ChoMySqlADODictionaryConfigObjectAdapter, ChoMySqlADODictionaryConfigStorage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" />
  </mySqlDictionarySectionHandlerTest>
  <configSections>
    <sectionGroup name="mySqlDictionarySectionHandlerTest">
      <section name="applicationSettings" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
    </sectionGroup>
  </configSections>
</configuration>

Cinchoo – Configuration framework – SharedEnvironment, part 1

Shared Environment Configuration

In a situation, where you want to keep and maintain configurations for each  environment (DEV, UAT and PROD etc) separately in a centralized location, attach them to your application and/or switch them at run-time, all these can be done using Cinchoo configuration framework. This is the first of the series of articles about SharedEnvironment in this blog.

SharedEnvironment.config file is a xml based configuration file shared by many applications. It contains configuration entries which controls the location of the application configuration locations and optional machines list for each environment etc.

Cinchoo framework discover this file (SharedEnvironments.xml) in the application base directory if not specified explicitly. It can be overridden by couple of ways

  • appFrxSettings – If the shared environment xml file is in a different location, you can specify them at sharedEnvironmentConfigFilePath attribute in App.config file as below
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	  <configSections>
    		<section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
  	</configSections>
	  <appFrxSettings sharedEnvironmentConfigFilePath="" appEnvironment="QA" />
</configuration>
  • Run-Time – In case where SharedEnvironment xml is not in a file or stored in different sources (Database, WebService etc), you can retrieve them and pass it to the framework by subscribing to ChoApplication.GetSharedEnvironmentConfigXml callback. Below is sample
class Program
{
    static void Main(string[] args)
    {
        ChoApplication.GetSharedEnvironmentConfigXml = new Func<string>(() =>
            {
                string xml = null;
                //Go to source and retrieve the xml
                return xml;
            });
    }
}
Now lets walk over the specification of SharedEnvironments.xml file.
<?xml version="1.0"?>
<configuration>
  <sharedEnvironment baseAppSharedConfigDirectory="C:\Config" defaultEnvironment="DEV">
    <environment name="DEV" comment="DEV Environment" appFrxFilePath="DEVConfig" >
      <machine>WNYC12D10101</machine>
      <machine>WNYC12D10102</machine>
      <machine>WNYC12D10103</machine>
      <machine>WNYC12D10104</machine>
    </environment>
    <environment name="PROD" comment="PROD Environment" appFrxFilePath="C:\Config\PROD\TestApp.config.txt" freeze="true" >
      <machine>SNYC12D10101</machine>
      <machine>100.39.191.175</machine>
    </environment>
    <environment name="UAT" comment="UAT Environment" >
      <machine>WNYC1108054621</machine>
      <machine>SNYC*</machine>
    </environment>
  </sharedEnvironment>
</configuration>
  • baseAppSharedConfigDirectory – A configuration directory used by an environment whose appFrxFilePath is not specified or it contains relative file path. In the above sample, the ‘DEV’ environment will create or use the ‘C:\Config\DEV’ directory to consume configuration files. The environment ‘UAT’ uses ‘C:\Config\UAT’ directory to read / store configuration. If not specified, it will be defaulted to application binary base directory.
  • defaultEnvironment – An environment used by the host which is not listed in this xml. In this sample, the ‘DEV’ environment will be used by the host not attached to any environment.
Each environment can be created using ‘environment’ element. In the above sample, we have ‘DEV’, ‘PROD’, and ‘UAT’ environments.
  • name – Name of the environment, mandatory.
  • freeze – true, all the hosts belongs to the environment locked. The applications hosted on these machines are locked to use this environment. By no means, it can be overridden via App.Config file in appFrxSettings/appEnvironment section. False, it can be overridden. Default value is false. Optional.
  • appFrxFilePath – A file / directory path. Either absolute / relative path. In case of relative path, framework resolves the path in relative to ‘baseAppSharedConfigDirectory’ path. In the sample above, for the ‘DEV’ environment, this path resolved to ‘C:\Config\DEVConfig’. If missing/not specified, the path will be the environment ‘Name’ under ‘baseAppSharedConfigDirectory’. In the sample above, the ‘UAT’ environment will resolve this path to ‘C:\Config\UAT’. It is optional.
Then you can bind hosts to any environment using ‘machine’ element under ‘environment’. Host can be either MachineName or IP Address. Also the those host names can contain wild card characters as well.
Ex: SYNC*, WNYC1002340? etc
Your application can be configured to use particular environment by specifying at appFrxSettings as below unless it is locked via Freeze attribute
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  	<configSections>
    		<section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
  	</configSections>
  	<appFrxSettings appEnvironment="QA" />
</configuration>

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