Cinchoo – Simplified Shell Context Menu Extensions development

Download Samples.zip – 14.5 KB

1. Introduction

Cinchoo is the application framework for .NET. One of the main functionality it provides to the users is application configuration management. It offers rich base framework classes to build .NET applications targeted on Windows platform.

In this article, I’m going to illustrate how to extend the Windows shell via context menu using Cinchoo library. These context menus must be for a specific file type, such as *.pdf files, drives, folders and more. This provides a way to customize Windows shell functionality through Windows Explorer. It is a simpler, fluent model for developing shell extension context menus rapidly. Making it as library letting you to concentrate yourself on the core development tasks.

2. Requirement

This functionality is written in C# for the .NET 4.0 Framework. It is part of Cinchoo framework, which is a great library with lot of features like Configuration Management, common ApplicationHost, Shell features etc.

3. “Hello World!” Sample

Lets begin by looking into a simple example of an creating shell extension application with ‘Hello World’ context menu attached to any file (*) type.

Figure 3.1 Screenshot of ‘Hello World’ shell context menu

ShellEx1

  • Download the latest Cinchoo binary here. (Nuget Command: Install-Package Cinchoo)
  • Open VS.NET 2010 or higher
  • Create a sample VS.NET (.NET Framework 4) WinForm/Console Application project
  • Add reference to Cinchoo.Core.dll
  • Use the Cinchoo.Core.Shell namespace
  • Copy and paste the below Shell Extension object

Listing 3.1 Defining Shell Extension Object

[ChoShellExtension]
public class HelloWorldShellExt
{
    [ChoShellExtensionContextMenu("*")]
    public static void HelloWorld(string[] args)
    {
        MessageBox.Show(args[0]);
    }
}

The code above illustrates about defining shell extension object. First thing define a shell extension class (ex. HelloWorldShellExt) and it must be decorated with ChoShellExtensionAttribute to complete the definition. Define a context menu method (ex. HelloWorld) decorated with ChoShellExtensionContextMenuAttribute. In this sample, this method is defined to be called for any file type (*). When a user right click on a file in the Windows Explorer and clicked the ‘Hello World’ context menu, this method will be called with the referenced filepath.

Next define a application host object as below,

Listing 3.2 Application host object

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
}

The purpose of this class is to enable your application self-installable shell extension program. The above code illustrates about defining one.

Finally, lets write the main entry code as below

Listing 3.3 Main Method

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

Thats all, your application is now ready to be installable as shell extension and called by Shell when context menu is clicked.

Listing 3.4 HelloWorld.exe with /#SE:Register

>HelloWorld.exe /#SE:Register

The above command line argument lets you register your application to the shell.

Listing 3.5 HelloWorld.exe with /#SE:Unregister

>HelloWorld.exe /#SE:Unregister

To unregister the application, pass /#SE:Unregister command line argument to the application.

4. Defining Shell Extension Object

4.1 Attributes

4.1.1 ChoShellExtension

This attribute is applied to the class containing shell extension context menu methods. This class will be discovered by Cinchoo framework during installation, invocation of shell extension methods.

4.1.2 ChoShellExtensionContextMenu

This attribute can be applied to the methods of the shell extension class to designate them as shell extension methods. This attribute designate the methods to receive the command when the user clicks on the context menu from Windows explorer. You can define multiple shell extension handler methods using this attribute for any file types.

4.1.2.1 FileType

Specify file type, such as *.txt files, file classes such as ‘text files’, folders, drives etc. to which the context menu handlers will be registered to.

4.1.2.2 MenuText

Specifies the context menu text. It is optional. If not specified, method name will be used as context menu text.

4.1.2.3 Icon

Specifies the context menu icon. It is optional. If not specified, application icon will be used.

4.1.2.4 IconIndex

If your application executable contains multiple icons, you can use one of them for context menu by specifing the icon index. There is no straight approach to add multiple icons to .NET application. But the below article explains the steps to add some

http://einaregilsson.com/add-multiple-icons-to-a-dotnet-application/

4.1.2.5 ShellKeyName

Specify the registry key name. It is optional. If not specified, method name will be used as shell key name.

4.1.2.6  AdditionalCommandLineArgs

Specify any additional context information required to process the request. It is optional. Framework automatically remembers and pass these information to the handler.

5. Installing Shell Extension Object

5.1 Self-Installable

If your application build using Cinchoo framework application host, your application is self-installable. Make sure to run the application in Administrator privilege mode in order to register it successfully.

First define a application host object as below

Listing 5.1 Application host object

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
}

The purpose of this class is to enable your application as self-installable shell extension program. Then write the main entry code as below to complete the application as self-installable program.

Listing 5.2 Main Method

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

5.2 Custom Installation

Some cases you may want to control and customize the installation of Shell Extension objects. This section will talk about the commands available to accomplish the installation of shell extension objects.

Cinchoo exposes methods via ChoShellExtension class to discover, register and unregister shell extension objects.

5.2.1 Register

This method used to discover all the shell extension objects available in your application and register them with the shell.

5.2.2 Unregister

This method used to unregister all the registered shell extension handlers.

Leave a comment