browse by category or date

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
a WordPress rating system
How To Add Right-Click Context Menu To Windows Explorer, 3.0 out of 5 based on 1 rating

Possibly relevant:

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Incoming Search

.net, c#, windows

No Comment

Add Your Comment