Click here to Skip to main content
6,292,426 members and growing! (9,900 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#.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXPVS.NET2003, Dev
Posted:12 Apr 2005
Views:75,975
Bookmarked:75 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
13 votes for this article.
Popularity: 4.32 Rating: 3.88 out of 5
2 votes, 15.4%
1
1 vote, 7.7%
2
1 vote, 7.7%
3
2 votes, 15.4%
4
7 votes, 53.8%
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


Member
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
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 43 (Total in Forum: 43) (Refresh)FirstPrevNext
GeneralAdd icon to the menu PinmemberMember 441027115:57 27 Apr '09  
Generaladding file,edit,view,help menus to an windows application Pinmemberaurosikhadas2:15 1 Oct '08  
GeneralIs it possible only windows xp and not 2000 Pinmemberhesaigo999ca4:38 24 Jul '08  
GeneralRe: Is it possible only windows xp and not 2000 Pinmemberdmihailescu13:52 24 Jul '08  
GeneralIt doesn't work for me [modified] PinmemberRoyce Fickling6:18 18 Jul '08  
GeneralRe: It doesn't work for me Pinmemberwrschmid6:18 20 Nov '08  
GeneralRe: It doesn't work for me PinmemberCodeMonkey854:25 3 Dec '08  
GeneralRe: It doesn't work for me Pinmemberdmihailescu14:08 3 Dec '08  
GeneralRe: It doesn't work for me PinmemberCodeMonkey8514:42 3 Dec '08  
GeneralIs it possible to added a menu hierarchy? Pinmembermaxx_delusional11:26 6 Jun '08  
GeneralRe: Is it possible to added a menu hierarchy? Pinmemberdmihailescu14:25 6 Jun '08  
GeneralRe: Is it possible to added a menu hierarchy? PinmemberGoran _12:43 2 Jul '08  
GeneralRegistry Entry throught Setup And Deployment Project PinmemberMohantaD7:57 23 Nov '07  
GeneralRe: Registry Entry throught Setup And Deployment Project Pinmemberdmihailescu14:27 6 Jun '08  
GeneralRe: Clicking on drive Pinmemberdae2200011:58 9 Oct '07  
Questioncontext menu in applications (notepad, word ..) [modified] Pinmemberbalu2341:11 19 Sep '07  
AnswerRe: context menu in applications (notepad, word ..) Pinmemberdmihailescu5:53 20 Sep '07  
QuestionRe: context menu in applications (notepad, word ..) Pinmemberbalu23419:52 20 Sep '07  
QuestionContext menu order Pinmembertwsabonrai4:50 19 Feb '07  
QuestionCommand arguments PinmemberMith-Randir4:49 26 Sep '06  
AnswerRe: Command arguments Pinmemberdmihailescu5:55 20 Sep '07  
Questionhow can i control in event handler Pinmembersyriast2:05 31 Jul '06  
Questionget code in vb.net Pinmembersyriast1:07 31 Jul '06  
AnswerRe: get code in vb.net Pinmemberdmihailescu5:50 20 Sep '07  
QuestionHow do I set up the registry key value to allow for multiple file arguments? PinmemberMartyP15:19 21 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
Web19 | Advertise on the Code Project