Tag Archives: C#

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

[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 – 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.

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

  • appFrxSettings – If you have a file contain shared environment xml in a different location, you can specify them here. Below is the sample
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
	</configSections>
	<appFrxSettings appFrxFilePath="" sharedEnvironmentConfigFilePath="" appEnvironment="QA" />
</configuration>
  • Run-Time – In cases 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;
            });
    }
}
Your application can be configured to use particular environment by specifying at appFrxSettings as below
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
	</configSections>
	<appFrxSettings appFrxFilePath="" sharedEnvironmentConfigFilePath="c:\Temp\NewSharedEnvironments.config" appEnvironment="QA" />
</configuration>
Now lets walk over the specification of SharedEnvironment.Config file.
<?xml version="1.0"?>
<configuration>
  <sharedEnvironment baseAppSharedConfigDirectory="C:\Config" defaultEnvironment="DEV">
    <environment name="DEV" freeze="true" appFrxFilePath="DEVConfig" >
      <machine>WNYC12D10101</machine>
      <machine>WNYC12D10102</machine>
      <machine>WNYC12D10103</machine>
      <machine>WNYC12D10104</machine>
    </environment>
    <environment name="PROD" appFrxFilePath="C:\Config\PROD\TestApp.config.txt" freeze="true" >
      <machine>SNYC12D10101</machine>
      <machine>100.39.191.175</machine>
    </environment>
    <environment name="UAT" >
      <machine>WNYC1108054621</machine>
      <machine>SNYC*</machine>
    </environment>
  </sharedEnvironment>
</configuration>
  • baseAppSharedConfigDirectory – A configuration directory used by a environment whose appFrxFilePath is not specified or it contains relative file path. In the above samples, environment ‘UAT’ uses this directory to read / store configuration. If not specified, it will be defaulted to application binary base directory.
  • defaultEnvironment – An environment used by any host which is not listed in this xml.
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. Can not be overridden by specifying it in appFrxSettings/appEnvironment. 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

Follow

Get every new post delivered to your Inbox.