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.

Writing to the Registry

So far we have read the file type key from the registry and checked if the file type is registered. We will now modify the registry to add our file type. It is important to note that our end user may be restricted by a security policy from making the modifications. In production code you should add error trapping and recover gracefully where this restriction occurs.

Assuming that we now know that our file type needs to be registered, we can use the same Registry and RegistryKey classes to create our file type entry. This entry includes a unique name to use for our file type, we will use the name, "blackwasp.test".

// No, so register it
RegistryKey rkNew;

// Create the registry key
rkNew = rkRoot.CreateSubKey(".blackwasp");

// Set the unique file type name
rkNew.SetValue("", "blackwasp.test");

When setting the value for the registry key, two strings are provided. The first is the name and the second is the value. In this case, the first string is empty to indicate that the default value for the key is to be set.

With the file type extension registered, a new registry key must be created beneath HKEY_CLASSES_ROOT. This holds the information relating to the new file type. It is named with the unique file type that has been associated with the file type extension. The default value for this key holds a user-readable description for the file type that will be displayed in Windows Explorer.

// Create the file type information key
RegistryKey rkInfo = rkRoot.CreateSubKey("blackwasp.test");

// Set the default value to the file type description
rkInfo.SetValue("", "BlackWasp Test File");

If you execute the code developed so far, you will see the two new keys in the registry. After execution, viewing a test file with the .blackwasp extension in Windows Explorer will allow you to see the file description. However, we have added no verbs as yet so nothing new will appear if you right-click the file.

Adding Verbs

To add a verb to the file type, we need two new keys. The first is "shell" and is a sub-key of the file type information key. This means it is one lower in the hierarchy than the keys we have added so far. Within the shell key we then add one new key for each verb that we want to add to our file type.

// Create the shell key to contain all verbs
RegistryKey rkShell = rkInfo.CreateSubKey("shell");

// Create a subkey for the "Open" verb
RegistryKey rkOpen = rkShell.CreateSubKey("Open");

The default value for the new verb determines its display name in shortcut menus. If not specified, the menu description will match the verb name. The code below adds the display name to enable the action in the menus and includes an ampersand (&) to indicate that the "O" is a shortcut key for the menu option.

// Set the menu name against the key
rkOpen.SetValue("", "&Open Document");

If you execute the code now, you will find that the shortcut menu for your test file contains a new menu item. (If you have already run the program you will need to delete the .blackwasp registry key or the condition that checks for its existence to update the file type registration.) However, selecting the new menu option simply raises an error.

We can add an action to the verb so that Windows knows what program to run when the option is selected. This is achieved by adding a "command" subkey beneath our verb key. The command includes the name of the application, including the full path to the executable. To add the filename of the selected file as a parameter we use the code "%1".

Assuming that our application lives in the root of the C: drive and is named "FileTypeSetter.exe", our command will be:

c:\FileTypeSetter.exe %1

Add the following code but substitute the correct file name and path according to the name and location of the program you have created so far.

// Create and set the command string
rkNew = rkOpen.CreateSubKey("command");
rkNew.SetValue("", @"c:\FileTypeSetter.exe %1");

We are now nearly finished! If you execute the code, again bypassing the conditional statement, you will find that the test file can be double-clicked to open our application. We can also use our new menu option to open the application.

We now need to modify the Main method of our application to check for the name of the document to load from its first start-up parameter. I explained how to do this in "Windows Forms Application Start-Up Parameters".

Finally, we can add an icon to the file type to give it a professional look. Icons are referenced by the name of the file that they exist within. If this is a true icon (.ico) file then the name itself will suffice. Where an executable or DLL contains the icon we need, the filename of the executable or DLL is required, suffixed with a comma and the icon number from the file. As you may not have an icon to hand, let's use the standard icon from Notepad. We need a new subkey of the file information key for this.

// Assign a default icon
rkNew = rkInfo.CreateSubKey("DefaultIcon");
rkNew.SetValue("", @"C:\windows\notepad.exe,0");

The program is now complete and ready for testing. Note that sometimes the default icon does not appear immediately and requires a restart of Windows Explorer to become visible. The functionality of the file type is always added immediately.

A Note for Vista Users

Vista's User Account Control (UAC) system prevents writing to the registry in this manner to normal users. To execute the sample code, the program must be compiled first. The executable file can then be right-clicked and the "Run as Administrator" option used to run the program with elevated privileges.

2 August 2006