BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Programmatically Checking and Setting File Types

When developing a Windows Forms application that loads documents, setting custom file types adds greatly to the professional feel of the software. This article explains how to programmatically check your file type is registered and register it if not.

Why Not Register during Set-Up?

Generally the installation package for an application will modify the registry of the end user's Windows installation and set up all of the file types that are required. This is an ideal place in which to perform this action. However, it can be problematic should the user remove or reconfigure the file type. Also, with Xcopy deployment, it is possible that no installation package will be executed. In these cases, another solution is required.

How are File Types Registered?

When a file type is registered, new items are inserted into the Windows registry. These items control how files of the new type will appear in Windows Explorer and how they will react to user input.

The key information that is required to be registered is as follows:

  • The file extension for the file type.
  • A unique name for the file type.
  • A descriptive, user-readable name for the file type.
  • An icon to display alongside files of the file type.

Once a file type is created, verbs are added to determine how the file type reacts to user input. Each verb appears on the pop-up menu shown when you right-click a file in Windows Explorer. eg. Run, Open, etc.

The key information for each verb is as follows:

  • A name for the verb.
  • A display name for the verb in menus, possibly including a short-cut key.
  • The command to execute when the verb is clicked.

This information can be registered programmatically using the .NET Framework. The remainder of this article describes the C# code required.

WARNING: This article deals with the registry, which is responsible for the configuration of your Windows system. Errors in the registry can leave your system inoperable. No responsibility for system damage can be accepted by BlackWasp should the methods described cause issues. If you do not accept responsibility for using the methods described here, you must not use them. It is advisable to create a recoverable backup of your system before making registry modifications.

Microsoft.Win32 Namespace

The Microsoft.Win32 namespace contains classes for responding to system events and for manipulating the operating system configuration. We will use this to modify the registry, add our file type and to check the registry to see if our file type exists. To make the sample code clear, include the following using directive:

using Microsoft.Win32;

Reading from the Registry

File types are found in the registry's HKEY_CLASSES_ROOT section. You can see this by running a registry editor program. To run the registry editor, click Start then Run and run the program "regedt32.exe".

Each file type is held as a registry key with a name that starts with a full stop (period). In this example, we will be testing for the existence of the file type ".blackwasp", which should not exist in your registry at this time,

To read from the registry, the Registry and RegistryKey classes are used. As the registry is organised in hierarchical tree structure, it can be useful to create a reference to the major section and then attempt to open the keys within the section.

// Create a registry key object to represent the HKEY_CLASSES_ROOT registry section
RegistryKey rkRoot = Registry.ClassesRoot;

// Attempt to retrieve the registry key for the .blackwasp file type
RegistryKey rkFileType = rkRoot.OpenSubKey(".blackwasp");

Following execution of this code, rkFileType will contain one of two things. If the file type is registered, its registry key information will be retrieved. If the file type does not exist, rkFileType will be null. We will check for null, as follows:

// Was the file type found?
if (rkFileType == null)
{
    // No, so register it
}

The above code performs a simple check only. If the .blackwasp key is not in HKEY_CLASSES_ROOT the file type is not registered. In reality, we should check that every piece of information we require is correctly registered, using the RegistryKey.GetValue method, and rectify every item where incorrect.

2 August 2006