Cinchoo – Simplified File Type Associations Development

Download sample – 80.7 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 your application to register itself to associate to a file type and how the application can receive the file information when double-clicked on a file from Windows Explorer, etc. 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. How It Works

File associations control the following functionality:

  • Which application launches when a user double-clicks a file
  • Which icon appears for a file by default
  • How the file type appears when viewed in Windows Explorer

Application developers can use file associations to control how the Shell treats custom file types, or to associate an application with existing file types. For example, when an application is installed, the application can check for the presence of existing file associations, and either create or override those file associations.

4. When You Should Implement File Associations

Applications can use files for various purposes: some files are used exclusively by the application, and are not typically accessed by users, while other files are created by the user and are often opened, searched for, and viewed from the Shell.

Unless your custom file type is used exclusively by the application, you should implement file associations for it. As a general rule, implement file associations for your custom file type if you expect the user to interact directly with these files in any way. That includes using the Shell to browse and open the files, searching the content or properties of the files, and previewing the files.

5. “Hello World” Sample

Let’s begin by looking into a simple example of an creating an application registering and opening the ‘.hlw’ files. The screenshot shows how our sample application registers itself to ‘.hlw‘ file types.

Figure 5.1 Screenshot of ‘.hlw’ file type association

When a user double clicks this file, our sample application will be launched and displays the window with the referenced file information.

Figure 5.2 Screenshot of sample application Main Window

  • 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

Listing 5.1 Defining File Associations

[assembly: ChoShellFileAssociation(".hlw", Description = "Hello World! Document")]

The above code illustrates how to specify file associations in the sample application using ChoShellFileAssociationAttribute assembly level attribute. It can be defined either in AssemblyInfo.cs file or any class definition file. In this example, it is defined to use ‘.hlk’ file types with optional ‘Hello World! Document’ description.

If you want to leverage the Cinchoo framework registration process, you must define and use ChoApplicationHost derived class as shown below.

Listing 5.2 Application host object

public class AppHost : ChoApplicationHost
    public override object MainWindowObject
            return new MainForm();

The purpose of this class is to enable your application self-installable file type association program.

Finally, let’s write the main entry code as below.

Listing 5.3 Main Method

class Program
    static void Main(string[] args)

That’s all. Your application is now ready to be register-able as application to open ‘.hlw’ files.

The sample runs below shows how to register/unregister the sample application to file types

Listing 5.4 FileTypeAssociationExample.exe with /#SE:Register

>FileTypeAssociationExample.exe /#SE:Register

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

Listing 5.5 FileTypeAssociationExample.exe with /#SE:Unregister

>FileTypeAssociationExample.exe /#SE:Unregister

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

6. ChoShellFileAssociationAttribute

This attribute is applied at the assembly level to the application to specify what file types are to be associated. You can have multiple definitions of this attribute in an application to register to multiple file types.

6.1 Extension

Specify file type, such as *.txt files, file classes such as ‘text files’, folders, drives, etc. to which the application will be registered to. Possible values can be ‘.hlw’ or ‘hlw’.

6.2 ProgID

The name uniquely identifies the file type. By default, ‘{Ext}File’ will be used as ProgID. If your application name isn’t short, specify abbreviation of it as ProgID. The proper format of a ProgID key name is [Vendor or Application].[Component].[Version], separated by periods and with no spaces, as in Word.Document.6.

6.3 Description

Short description of the file type. By default, ‘{Ext} Document’ will be used as description.

6.4 DefaultIcon

Path to the icon file to set as default icon that you want to display for file types associated with this ProgID. By default, your application icon will be set as default icon.

6.5 DefaultIconIndex

If your application executable contains multiple icons, you can use one of them as default icon to the file type associated by specifying 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:

6.6 AdditionalCommandLineArgs

Specify any additional command line arguments required when the associated application launched. It is optional.

6.7 DefaultArgSwitch

Optional command line argument switch you want to specify to the default context file argument.

7. Registration Steps

7.1 Self-Installable

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

First, define an application host object as below.

Listing 7.1.1 Application host object

public class AppHost : ChoApplicationHost

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

Listing 7.1.2 Main Method

class Program
    static void Main(string[] args)

7.2 Custom Installation

In some cases, you may want to control and customize the registration process. This section will talk about the commands available to accomplish the installation of file type associations.

Cinchoo exposes methods via ChoShellFileAssociation class to discover, register and unregister file associations.

7.2.1 Register

This method is used to discover all the file type association definitions available in your application and register them with the shell.

7.2.2 Unregister

This method is used to unregister all the registered file type associations.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s