Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C#
Article

Add a context menu to the Windows Explorer

Rate me:
Please Sign up or sign in to vote.
4.67/5 (39 votes)
12 Apr 20051 min read 368.5K   13.1K   148   74
Add a context menu to the Windows Explorer using Registry only.

Image 1

Image 2

Introduction

Many of us like the convenience of the context menus in Windows Explorer and hate typing long paths in the command prompt window. This tool is going to address this issue by adding a new item on the context menu when you right click on a folder. You could get a command prompt that will open in the parent directory and not elsewhere. If you are not interested in the command prompt, then you can specify another executable of your choice as the target and still save some clicks and typing.

Background

Dealing with Explorer usually requires some shell programming that is not always nice. Fortunately, the hack I'm going to describe would merely set some registry entries to make this happen.

Using the code

The registry entries are the following:

C#
private const string MenuName = "Folder\\shell\\NewMenuOption";
private const string Command = "Folder\\shell\\NewMenuOption\\command";

The code is self-explanatory and it merely sets the registry:

C#
private void btnAddMenu_Click(object sender, System.EventArgs e)
{
    RegistryKey regmenu = null;
    RegistryKey regcmd = null;
    try
    {
        regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
        if(regmenu != null)
            regmenu.SetValue("",this.txtName.Text);
        regcmd = Registry.ClassesRoot.CreateSubKey(Command);
        if(regcmd != null)
                regcmd.SetValue("",this.txtPath.Text);
    }
    catch(Exception ex)
    {
        MessageBox.Show(this,ex.ToString());
    }
    finally       
    {
        if(regmenu != null)
            regmenu.Close();
        if(regcmd != null)
            regcmd.Close();
    }        
}

Or it deletes the settings if you don't like them anymore:

C#
private void btnRemoveMenu_Click(object sender, System.EventArgs e)
{
    try
    {
        RegistryKey reg = Registry.ClassesRoot.OpenSubKey(Command);
        if(reg != null)
        {
            reg.Close();
            Registry.ClassesRoot.DeleteSubKey(Command);
        }
        reg = Registry.ClassesRoot.OpenSubKey(MenuName);
        if(reg != null)
        {
            reg.Close();
            Registry.ClassesRoot.DeleteSubKey(MenuName);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(this,ex.ToString());
    }
}

The rest of the code only provides a sane user interface.

Points of Interest

You obviously have all the rights to change the Registry for this application to run as desired.

History

  • 12th April, 2005 - version 1.0.0.0.

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


Written By
Software Developer (Senior)
United States United States
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

Comments and Discussions

 
GeneralRegistry Entry throught Setup And Deployment Project Pin
MohantaD23-Nov-07 6:57
MohantaD23-Nov-07 6:57 
GeneralRe: Registry Entry throught Setup And Deployment Project Pin
dmihailescu6-Jun-08 13:27
dmihailescu6-Jun-08 13:27 
GeneralRe: Clicking on drive Pin
dae220009-Oct-07 10:58
dae220009-Oct-07 10:58 
Questioncontext menu in applications (notepad, word ..) [modified] Pin
balu23419-Sep-07 0:11
balu23419-Sep-07 0:11 
AnswerRe: context menu in applications (notepad, word ..) Pin
dmihailescu20-Sep-07 4:53
dmihailescu20-Sep-07 4:53 
QuestionRe: context menu in applications (notepad, word ..) Pin
balu23420-Sep-07 18:52
balu23420-Sep-07 18:52 
QuestionContext menu order Pin
twsabonrai19-Feb-07 3:50
twsabonrai19-Feb-07 3:50 
QuestionCommand arguments Pin
Mith-Randir26-Sep-06 3:49
Mith-Randir26-Sep-06 3:49 
I need to pass some arguments (this includes the path to the right-clicked file) to the program that'll fire up when slecting the menu item that i create.
Please help.
Excuse my english

All we have to decide is what we do with the time that is given to us.
All that once was is lost for few now live who remeber it.
I know that you have other rules, but I obey no rule.

AnswerRe: Command arguments Pin
dmihailescu20-Sep-07 4:55
dmihailescu20-Sep-07 4:55 
GeneralRe: Command arguments Pin
Onur Guzel15-Nov-10 5:02
Onur Guzel15-Nov-10 5:02 
GeneralRe: Command arguments Pin
dmihailescu15-Nov-10 15:13
dmihailescu15-Nov-10 15:13 
Questionhow can i control in event handler Pin
syriast31-Jul-06 1:05
syriast31-Jul-06 1:05 
Questionget code in vb.net Pin
syriast31-Jul-06 0:07
syriast31-Jul-06 0:07 
AnswerRe: get code in vb.net Pin
dmihailescu20-Sep-07 4:50
dmihailescu20-Sep-07 4:50 
QuestionHow do I set up the registry key value to allow for multiple file arguments? Pin
MartyP21-Apr-06 14:19
MartyP21-Apr-06 14:19 
AnswerRe: How do I set up the registry key value to allow for multiple file arguments? Pin
MartyP29-Apr-06 9:54
MartyP29-Apr-06 9:54 
GeneralAdd an icon for the context menu entry Pin
FrEaK_CH1-Jan-06 13:19
FrEaK_CH1-Jan-06 13:19 
GeneralRe: Add an icon for the context menu entry Pin
MartyP29-Apr-06 9:52
MartyP29-Apr-06 9:52 
QuestionHow do i write for only JPG/JPEG images Pin
17-Oct-05 3:11
suss17-Oct-05 3:11 
AnswerRe: How do i write for only JPG/JPEG images Pin
Anonymous18-Oct-05 16:41
Anonymous18-Oct-05 16:41 
QuestionHow do I get the folder selected? Pin
Marlun2-Oct-05 11:45
Marlun2-Oct-05 11:45 
AnswerRe: How do I get the folder selected? Pin
Anonymous4-Oct-05 16:28
Anonymous4-Oct-05 16:28 
GeneralRe: How do I get the folder selected? Pin
Marlun4-Oct-05 19:18
Marlun4-Oct-05 19:18 
QuestionRe: How do I get the folder selected? Pin
SimonKnight660029-Mar-06 6:40
SimonKnight660029-Mar-06 6:40 
AnswerRe: How do I get the folder selected? Pin
SimonKnight660030-Mar-06 4:34
SimonKnight660030-Mar-06 4:34 

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.