Click here to Skip to main content
15,798,672 members
Articles / Programming Languages / C++

Customize explorer's devious context menu

Rate me:
Please Sign up or sign in to vote.
3.13/5 (16 votes)
2 Feb 2006CPOL2 min read 53K   1.3K   24   10
Do you want to add a menu item in windows explorer's context menu? then this article will show you easy way.


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.


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. <comment title="----------------------------- That's it! -------------------------" xmlns="">


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 5 Pin
David Furshpan6-May-11 9:50
David Furshpan6-May-11 9:50 
QuestionHow to add context-menu for selected text??? Pin
ashish1482@gmail.com14-Sep-10 9:19
ashish1482@gmail.com14-Sep-10 9:19 
QuestionGreat and thanks for documenting the registry entries. What about setting the default icon? Pin
Member 125885110-Dec-09 9:31
Member 125885110-Dec-09 9:31 
Generalcannot creat multiple menus for a specific File Type Pin
ahmasdrathore26-Oct-09 3:40
ahmasdrathore26-Oct-09 3:40 
GeneralCustomize explorer's devious context menu Pin
Member 473197413-Apr-08 22:25
Member 473197413-Apr-08 22:25 
GeneralThanks! Pin
TyronM1-Apr-06 11:01
TyronM1-Apr-06 11:01 
Questionwhat an article... Pin
Thomas Weidenmueller2-Feb-06 6:10
Thomas Weidenmueller2-Feb-06 6:10 
AnswerRe: what an article... Pin
NewtonVer22-Feb-06 19:45
NewtonVer22-Feb-06 19:45 
Smile | :)

I will check what is wrong though I am able to do download and see pics...

Generalbroken links... Pin
toxcct2-Feb-06 6:02
toxcct2-Feb-06 6:02 
GeneralRe: broken links... Pin
NewtonVer22-Feb-06 19:59
NewtonVer22-Feb-06 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.