2011-06-04

Copy a file's name to the clipboard from the context menu

Motivation

Often I need to mix between the regular explorer style file system navigation and various command line tools. For that purpose it is convenient to be able to get a file's name into the clipboard through an option in the explorer context menu of the particular file. Unfortunately the obvious echo "%1"|clip doesn't seem to use unicode charsets.

Solution

A small .Net tool will do the trick:

using System;
using System.Windows.Forms;
using System.Threading;

public class string2cb
{
    [STAThread]
    static void Main(string[] args)
    {
        Clipboard.SetText(args[0]);
    }
}

If you put the above code into a file called string2cb.cs you can compile it into an .exe file using the C# compiler that comes included with your .Net Framework:

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe string2cb.cs

Using the context menu from the previous post you can add the new function as follows:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\020string2cb]
"MUIVerb"="Full path to clipboard"

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\020string2cb\command]
@="C:\PathToMyusefultoys\string2cb.exe \"%1\""

Obviously you have to replace C:\PathToMyusefultoys\string2cb.exe with the path to the “install”-location of the compiled .exe file.

2011-05-22

Cascading context menus via static registry entries and ExtendedSubCommandsKey in Windows 7

The Problem

If you have been looking for a way to add cascading menus to the Windows 7 Explorer using static registry entries you may have found this article on MSDN. It is either too obscure to understand or wrong, or both, at least in the version dated 5/3/2011.

The description below was derived from looking at what registry accesses Explorer performs when you open a context menu (using MS / Sysinternals Process Explorer).

The Solution

First you create an “anchor” entry that will be added to the context menu:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\myusefultools_anchor]
"MUIVerb"="My Useful Tools"
"ExtendedSubCommandsKey"="*\\ContextMenus\\myusefultools"

If ExtendedSubCommandsKey is set to “foo\bar” then Explorer will look for the key “HKEY_CLASSES_ROOT\foo\bar\Shell”. Under that key it expects one subkey for each entry, e.g. (using the above value for ExtendedSubCommandsKey):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\001cmdhere]
"MUIVerb"="Open CMD here"

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\001cmdhere\command]
@="cmd.exe"

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\010morecmd]
"MUIVerb"="Another Command"

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\010morecmd\command]
@="cmd.exe"

Random stuff

Add "Position"="Bottom" to the anchor entry if you want it to show up at the bottom of the context menu.

The submenu entries are sorted according to the name of their registry key. I added 0XX to the names to force a specific order. As described in the MSDN article the CommandFlags value can be added (using the codes listed here) to customize the menu entries and create separators.

[HKEY_CLASSES_ROOT\*\ContextMenus\myusefultools\Shell\005separator]
"CommandFlags"=dword:00000008