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\"");
}
|