For several programs, I've wanted to be able to add a shell context menu item for a file that runs an exe. After some research, I came up with the following information. This menu is controlled by certain registry entries under the HKCR (Classes Root) entry. In HKCR, there are two relevant entries for each file extension: the actual extension, and the handler for that extension. For .bmp, for example, the extension key is as follows:
The key part of the extension key is the default string value that points to the handler for this extension, in this case Paint.Picture. Here's the handler:
To add a new context menu item, simply add a new key to the shell subkey of the handler key. Name it whatever you want. The default string value for the key will be the caption of the menu item. To specify the command to run, add a command subkey to your menu item key. The default string value of this key should be the path to your application. You can append "%1" to pass the filename of the file to your app.
I've written a registry helper class that encapsulates all of this, allowing you to create a handler with just one line of code. You can call it as follows:
- ".ext" is the extension you want to add a context menu to
- "commandname" is the name of the command
- "caption" is the caption to display in the context menu
- the boolean in the last parameter determines whether to create a system-wide context menu extension, or to register it for just the current user
This overload will determine what the path is to the currently running assembly, or you can use one of the other overloads that allows you to specify the path.
Today, I ran up against a situation where I needed to create a generic type dymnamically, e.g. to specify what type the object was built around on the fly. I spent a while trying several different methods (Reflection.Emit, Lightweight Code Generation (LCG), etc.), but it turns out I was dramatically over-engineering the problem. In .NET 2.0, there is a new Type.MakeGenericType method that returns a new type that accepts a paramarray of types and returns a generic type built from those parameters.
Here is the basic usage, using the generic List<T> class for an example:
Type genericParameterType = typeof(string);
Type genericType = typeof(List<>).MakeGenericType(genericParameterType);
ConstructorInfo constructor = genericType.GetConstructor(null);
Update: Just released a small tweak to support the latest versions of WLM.
I've been using Yahoo! Music Unlimited for almost a year now. It's a great service. Cheap ($4.99/mo), fairly good selection, and 192kbs quality. The only problem is, it doesn't integrate with MSN Messenger/Windows Live Messenger's now playing display. There is a third-party plugin that sort of provides this functionality, but it's old, hasn't been updated, and doesn't work properly with the latest versions of WLM.
So I pulled together some information and code from a few different sources and whipped up my own plugin, using C# and VS 2005. It is extremly straightforward. Just import the interop libs, implement and interface, and register the assembly in the proper place.
The WLM now playing integration is fairly straightforward as well, if undocumented. It uses the WM_COPYDATA message to pass a specially formatted string to Messenger. I have factored the two segments seperately, so it should be easy to swap out the YME or WLM integration to support other music players or messaging services.
Feel free to use the component, or peek at the code, as long as you provide attribution back to me.
Requires .NET 2.0 Runtime. You can get it here, or as a standard windows update.