2010
10.21

I was looking for a way to add my program into Windows Explorer right-click context. So I googled it, and I think I found what I was looking for.

As usual, StackOverflow delivers the best clue. I found this discussion that gave me the starting point.

So the idea here is to use Windows’ registry. Apparently there’s a location where we can register/unregister the context menu.

Thanks polyglot & StackOverflow!

These are the required assemblies to interop with Windows.

using System;
using Microsoft.Win32;

The functions to register/unregister the context menu :

string contextName = "contextTest";
public bool Register()
{
	//Open HKEY_CLASSES_ROOT, Key *, last param is to make it writable
	RegistryKey reg = Registry.ClassesRoot.OpenSubKey("*", true);
	if (reg != null)
	{
		RegistryKey shell = reg.OpenSubKey("shell", true);
		if (shell != null)
		{
			//Get Current Assembly fullname
			string appLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
			
			RegistryKey ctx = shell.OpenSubKey(contextName);
			if (ctx != null)
				return true;
			else
				ctx = shell.CreateSubKey(contextName);
			ctx.SetValue("","Context It");
			RegistryKey cmd = ctx.CreateSubKey("command");
			cmd.SetValue("", """ + appLocation +"" "%L"");
			return true;
		}
	}
	return false;
}
public bool Unregister()
{
	RegistryKey reg = Registry.ClassesRoot.OpenSubKey("*", true);
	if (reg != null)
	{
		RegistryKey shell = reg.OpenSubKey("shell", true);
		if (shell != null)
		{
			shell.DeleteSubKeyTree(contextName);					
			return true;
		}
	}
	return false;
}

The result:
right.contex

GD Star Rating
loading...
How To Add Right-Click Context Menu To Windows Explorer, 3.0 out of 5 based on 1 rating

Incoming Search Term

Advertise Here

No Comment

Add Your Comment