5,782,627 members and growing! (18,988 online)
Email Password   helpLost your password?
Languages » C# » Samples     Intermediate

Add a context menu to the Windows Explorer

By dmihailescu

Add a context menu to the Windows Explorer using Registry only.
C#Windows, .NET, .NET 1.0, .NET 1.1, .NET 2.0, NT4, Win2K, WinXPVS.NET2003, Visual Studio, Dev

Posted: 12 Apr 2005
Updated: 12 Apr 2005
Views: 64,274
Bookmarked: 69 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.10 Rating: 3.80 out of 5
2 votes, 16.7%
1
1 vote, 8.3%
2
1 vote, 8.3%
3
2 votes, 16.7%
4
6 votes, 50.0%
5

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:

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:

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:

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

About the Author

dmihailescu


Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.
Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 42 (Total in Forum: 42) (Refresh)FirstPrevNext
Generaladding file,edit,view,help menus to an windows applicationmemberaurosikhadas2:15 1 Oct '08  
GeneralIs it possible only windows xp and not 2000memberhesaigo999ca4:38 24 Jul '08  
GeneralRe: Is it possible only windows xp and not 2000memberdmihailescu13:52 24 Jul '08  
GeneralIt doesn't work for me [modified]memberRoyce Fickling6:18 18 Jul '08  
GeneralRe: It doesn't work for mememberwrschmid6:18 20 Nov '08  
GeneralRe: It doesn't work for mememberCodeMonkey854:25 3 Dec '08  
GeneralRe: It doesn't work for mememberdmihailescu14:08 3 Dec '08  
GeneralRe: It doesn't work for mememberCodeMonkey8514:42 3 Dec '08  
GeneralIs it possible to added a menu hierarchy?membermaxx_delusional11:26 6 Jun '08  
GeneralRe: Is it possible to added a menu hierarchy?memberdmihailescu14:25 6 Jun '08  
GeneralRe: Is it possible to added a menu hierarchy?memberGoran _12:43 2 Jul '08  
GeneralRegistry Entry throught Setup And Deployment ProjectmemberMohantaD7:57 23 Nov '07  
GeneralRe: Registry Entry throught Setup And Deployment Projectmemberdmihailescu14:27 6 Jun '08  
GeneralRe: Clicking on drivememberdae2200011:58 9 Oct '07  
Questioncontext menu in applications (notepad, word ..) [modified]memberbalu2341:11 19 Sep '07  
AnswerRe: context menu in applications (notepad, word ..)memberdmihailescu5:53 20 Sep '07  
QuestionRe: context menu in applications (notepad, word ..)memberbalu23419:52 20 Sep '07  
QuestionContext menu ordermembertwsabonrai4:50 19 Feb '07  
QuestionCommand argumentsmemberMith-Randir4:49 26 Sep '06  
AnswerRe: Command argumentsmemberdmihailescu5:55 20 Sep '07  
Questionhow can i control in event handlermembersyriast2:05 31 Jul '06  
Questionget code in vb.netmembersyriast1:07 31 Jul '06  
AnswerRe: get code in vb.netmemberdmihailescu5:50 20 Sep '07  
QuestionHow do I set up the registry key value to allow for multiple file arguments?memberMartyP15:19 21 Apr '06  
AnswerRe: How do I set up the registry key value to allow for multiple file arguments?memberMartyP10:54 29 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Apr 2005
Editor: Rinish Biju
Copyright 2005 by dmihailescu
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project