Associating a File Extension and Context Menus with an Application in the Registry

You can easily assign a file extension to open with your application in the registry.

Verbal Description
The extension is added as a key under “HKEY_CLASSES_ROOT” with the (default) value set to the file’s type (a string arbitrarily assigned by yourself). Then you add a key with the file’s type also to “HKEY_CLASSES_ROOT” with its (default) value set to the file’s type description (that is shown in Exporer). Under the same key, add they keys “shell/open/command” with the (default) value being the program to execute. Use “%1” with the quotes to pass the file’s name to your app. You can also assign an icon by adding a “DefaultIcon” key under the file’s type key. Then additional context menu items can be added by putting keys the [filetype]/shell/[MenuItem]/Command

Example Registry Entries

  • HKEY_CLASSES_ROOT\.npost = NovaBlog.Post
  • HKEY_CLASSES_ROOT\NovaBlog.Post = NovaBlog Article Posting
  • HKEY_CLASSES_ROOT\NovaBlog.Post\DefaultIcon = C:\Program Files\NovaBlog\NovaBlox.exe,0
  • HKEY_CLASSES_ROOT\NovaBlog.Post\shell\Open = &Open
  • HKEY_CLASSES_ROOT\NovaBlog.Post\shell\Open\Command = C:\Program Files\NovaBlog\NovaBlox.exe “%1”
  • HKEY_CLASSES_ROOT\NovaBlog.Post\shell\PostNow = &Post the Article Now
  • HKEY_CLASSES_ROOT\NovaBlog.Post\shell\PostNow\Command = C:\Program Files\NovaBlog\NovaBlox.exe /postnow “%1”

C# Code
Thankfully there is a Microsoft.Win32 namespace with classes for working with the registry. This is all there is to it!

// Created by Noah Coad, 6/11/04 using Microsoft.Win32; using System.Windows.Forms; public static void CreateRegEntries() { RegistryKey rkey = Registry.ClassesRoot; rkey.CreateSubKey(".npost").SetValue("", "NovaBlog.Post"); rkey = rkey.CreateSubKey("NovaBlog.Post"); rkey.SetValue("", "NovaBlog Article Post"); rkey.CreateSubKey("DefaultIcon").SetValue("", Application.ExecutablePath + ",0"); rkey = rkey.CreateSubKey("shell"); RegistryKey keycmd = rkey.CreateSubKey("Open"); keycmd.SetValue("", "&Open"); keycmd.CreateSubKey("Command").SetValue("", "\"" + Application.ExecutablePath + "\" \"%1\""); keycmd = rkey.CreateSubKey("PostNow"); keycmd.SetValue("", "&Post Article Now"); keycmd.CreateSubKey("Command").SetValue("", "\"" + Application.ExecutablePath + "\" /postnow \"%1\""); }

3 thoughts on “Associating a File Extension and Context Menus with an Application in the Registry

  1. It’s more difficult if you try to grab the TIF association, though. After some digging I had to change the Explorer FileExts key, but I’m still not sure I did it right.

  2. Thanks for useful code snippet.

    If I would like to use a different DefaultIcon other than current defaulted in the application, let’s say in a resource assembly file (myResource.dll contains multiple icon files), how can I identify the resource ID it should point to?

    rkey.CreateSubKey(“DefaultIcon”).SetValue(“mypath/myResource.dll” + “,?”);

    Thanks.

  3. If you are concerned to learn Web optimization techniques then you have to read this piece of writing, I am sure you will get much more from this post on the topic of Web optimization.

Leave a Reply

Your email address will not be published. Required fields are marked *