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.