Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Customize explorer's devious context menu

0.00/5 (No votes)
2 Feb 2006 1  
Do you want to add a menu item in windows explorer's context menu? then this article will show you easy way.

Introduction

Say, you have developed web based File Management System (FMS), where users can upload files. Your FMS will catalog all files and allow users to search & group uploaded files. If I am a user of such FMS, what I would like that I can right click on any file in windows explorer where it shows me menu item for sending file to FMS.

Image showing customized context menu

Say, you have built encryption application which can encrypt any file or file folder. Again, I would like if you do some magic to allow me to specify file/folder to be encrypted in context menu of windows explorer.

Say, you have created a command line tool, which does something with objects appearing in windows explorepr (files, folders, drives, network shares etc). If you can provide context menu item for your command line tool, your users will be happy - as they will not need to traverse folders at command line. Check this.

Background 

There are two ways of doing things. Easy way and toguh way. (sorry, if you do not like such openings :))

Let us see.

Tough way: Windows explorer knows COM. If you implement some interfaces like IContextMenu in a COM dll and then you register your dll with Windows registry in a way explorer understands, then you can make context menu do anything you want. The problem is you do not know COM! Bigger problem is explorer does not know .NET CLR yet! That translates that you do not want to do not want to be burderned with complex things ;-) Go here to know more.

Easy way: Windows explorer knows registry. you know C#. And .NET knows Registry. Whenever you rightclick in explorer, it looks into registry at a location to find custom context menu items (thanks to MS engineers, who forethought). If it finds one, then it displays it. If you choose that item, filepath is passed as command line parameter to specified exe.

I have gone easy way, and have created a simple class for you which will allow you to customize context menu programatically. As a proof of concept, I have created a small winforms project which will allow your to cusotmize context menu manually.

Using the code

Class is called ShellContextMenu.

Constructor accets Unique name which will be used to name registry key.

private ShellContextMenu m_scm = new ShellContextMenu("Newtonica.Utility.ContextMenu");

You simply then call public methods of instance to add, read or remove context menu items.

m_scm.SetContextMenuItem(ShellContextMenu.ContextMenuFor.ALLFILES,"Send to FMS...", "MyFMS.exe %1%", "");
m_scm.RemoveContextMenuItem(ShellContextMenu.ContextMenuFor.ALLFILES, ""); 

To understand it clearly, go through source code.

If you compliel exe, here is how it will look. (and work hopefully, too.). As I do not have FMS.exe, I have taken notepad.exe as place holder.

Screen of application

Points of Interest

1. To have complete flexibility, you need to implement COM interfaces tough way, and it is worth it.
2. If simple way is enough for the purpose, do not overkill. 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here