Most of the real world applications need custom file extensions. Double clicking the file with such extension should be opened with particular application. This article explaining how to register a custom file extension in Windows 7. Custom file extensions normally resides in registry under the following key,

HKEY_CLASSES_ROOT

For an example, let me take an application named MyFoo.exe which need a custom file with extension “*.foo”


Step 1 : Create a new key under HKEY_CLASSES_ROOT with extension name (.foo).

Edit the default value of the key and set it as “MyFoo”, the application name.

Step 2 : Create another key under the same root with the name of the app “MyFoo”.

Step 3 : Create another sub node under MyFoo node and name it as DefaultIcon. Edit the default value and set the path to the icon file you want to add for your custom file.

Step 4 : Create another sub node under MyFoo node with the name of “shell”. This node is responsible for adding commands.

Step 5 : Under shell node, add your commands. Most of the traditional desktop applications came up with commands like open, play, preview, print, etc. For simplicity let me explain how to add a open command to this application. The MyFoo node should look like this once you have added the needed keys.

Now edit the command key and map the default value to the application exe.

Now we are done with registering the extension. All the files in the machine with .foo extension will be opened in MyFoo application. Also you can find the “Open” command on the context menu of the application tile.

Everything is fine. But how your application would know the location of the file which is opened? Using Command line arguments we can extract the path. In WPF, we can get the command line arguments in Application Start up event in this way.

            if (e.Args.Count() > 0)
            {
                this.Properties["FilePath"] = e.Args[0];
            }

The file path we stored here can be used anywhere in the application,

string filepath = Application.Current.Properties[“FilePath”].ToString();

But to make this work, the default value of the command should be modified slighter."C:\Users\labuser\Desktop\MyFoo\bin\Debug\MyFoo.exe" "%1".

Happy coding!!!…