Add Custom items to the Windows Explorer ContextMenu

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
9 posts Page 1 of 1
Contributors
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Hey guys.

This tutorial will show you how to add an item to the Windows Explorer file contextmenu which will link the file with your program on the event that a user clicks your custom item. This functionality will be very similar the way 7zip or WinRAR add items to your explorer contextmenu for the purposes of compression.

I'll be showing you how to do this from a VB program.

Firstly, go into Visual Studio (or VB Express), and create a new Windows Forms Application. Go into the "Solution Explorer", and add a new class to your project. Call it "ShellMenu". In the ShellMenu class, add this code:
Code: Select all
Imports Microsoft.Win32
NotInheritable Class ShellMenu

    Private Sub New()
    End Sub
    Public Shared Sub Register(ByVal fileType As String, ByVal shellKeyName As String, ByVal menuText As String, ByVal menuCommand As String)
  Dim regPath As String = String.Format("{0}\shell\{1}", fileType, shellKeyName)
  Using key As RegistryKey = Registry.ClassesRoot.CreateSubKey(regPath)
    key.SetValue(Nothing, menuText)
  End Using
  Using key As RegistryKey = Registry.ClassesRoot.CreateSubKey(String.Format("{0}\command", regPath))
    key.SetValue(Nothing, menuCommand)
  End Using
    End Sub

    Public Shared Sub Unregister(ByVal fileType As String, ByVal shellKeyName As String)
  Try
    Dim regPath As String = String.Format("{0}\shell\{1}", fileType, shellKeyName)
    Registry.ClassesRoot.DeleteSubKeyTree(regPath)
  Catch
  End Try
    End Sub
End Class
I'll explain what this does specifically at the end of the tutorial, but the bottom line is that it adds values to the classes root region registry, which is what controls the ContextMenuItems on the Windows Explorer ContextMenu.

Now go to "Form1.vb", and add a new button. On the Button1_Click event, add this code:
Code: Select all
ShellMenu.Register("txtfile", "CustomIntegrationTest", "Explorer integration test App", Application.ExecutablePath & " ""%1""")
Finally, go into the Form1_Load event, and add this code:
Code: Select all
If Environment.GetCommandLineArgs.Count <> 1 Then
    For Each itm In Environment.GetCommandLineArgs
    If itm <> Application.ExecutablePath Then
    MsgBox("We can do anything we want with the file: " & itm)
    End If
     Next
  End If
And that's it. Run your project, and click the button. Now right click any .txt file in Windows Explorer, and a custom item should be visible named "Explorer Integration test App". If you click it, the program will be launched and will say "We can do anything we want with the file: " and the file path of whatever item you clicked.

Here's a gif of how the program should work when completed:
Image

The short rundown:
The "ShellMenu" class lets us register (and unregister if needsbe) any contextmenu for any filetype in Windows Explorer. The register command works in the following way:
You can call: Register(The FileType, The name of your Program, What you want the custom menuitem to say, the command for the menuitem to execute when clicked)
A quick note on the filetype: You can't just enter ".txt" or ".jpg". The way that the registry handles file types is slightly more complicated than that. In the registry, ".txt" points to "txtfile", and ".jpg" points to "jpegfile". So if we wanted to register a contextmenuitem on all .jpg files, we'd use:
Code: Select all
ShellMenu.Register("jpegfile", "CustomIntegrationTest", "Explorer integration test App", Application.ExecutablePath & " ""%1""")
Our Button1 just acts to register our contextmenu for all text files. This shouldn't really need explaining much.

In the Form_Load Event, we look for all the Command Lines which have been sent to the program at the time of execution. The first command line will always be the application's executable path, and for this reason we ignore it. All the other Command Lines will have been sent by our custom menuitem. For this reason, we tell the user that they have been sent. Once we have these command lines, we can open the files that they point to, and do anything we want with them (open, edit, delete, upload, whatever).

The long rundown (if you're still confused after the short rundown, read this!)
So, in depth, what is the actual process of adding items to the Windows Explorer contextmenu?
Well, in the registry, "HKEY_CLASSES_ROOT" handles a lot of stuff, such as file associations and types. It also handles extra ContextMenuItems for files on a per-file type basis. In HKEY_CLASSES_ROOT, there is a key for each file type known by the system. There is also a key for each group of file types (e.g, .txt and .log are both a plaintext format, so the entries for ".txt" and ".log" are both "txtfile"). What the program does is add an extra item for any group of files which you want.
When the ContextMenu is pressed on the file type, a command line is passed to our program. For more information on command lines, please go here.

If you still don't understand how it works after this tutorial, PM me, and I'll be happy to help. Please be aware that this is not an easy topic to get your head around.

Regards,
Skillful
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Great tutorial! This is something that its hard to find elsewhere I think cooll;
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

:lol: i have used this function in my old apps
a few years back.
but nice tut :)
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

MrAksel wrote:
Great tutorial! This is something that its hard to find elsewhere I think cooll;
i saw something similar on codeproject.com but this looks easier. nice tut cooll;
You can find me on Facebook or on Skype mihai_92b
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

I'm glad you'll liked this.
More tutorials coming this month guys! :D
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
anthonygz
Member
Member
Posts: 48
Joined: Sun Feb 13, 2011 7:25 pm

Very nice tutorial Skillful!
You really do live up to your name.
User avatar
AnoPem
VIP - Donator
VIP - Donator
Posts: 441
Joined: Sat Jul 24, 2010 10:55 pm

I have made it so when i right click a folder it shows the menu item, can i add so when i click the menu item it will copy the folder name to a textbox in my form?
https://t.me/pump_upp
User avatar
polas
New Member
New Member
Posts: 13
Joined: Sun Dec 20, 2009 6:32 pm

Would not it be better to do this through the use of the path textbox ?
so we could upload any program i context menu, and how to remove it without problems.
User avatar
polas
New Member
New Member
Posts: 13
Joined: Sun Dec 20, 2009 6:32 pm

how to add custom exe to windows desktop context menu using 2 textboxes and 2 buttons.

button1 - add to context menu.
button2 - remove from context menu
Add with icons.

how about that ?
9 posts Page 1 of 1
Return to “Tutorials”