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
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:
Now go to "Form1.vb", and add a new button. On the Button1_Click event, add this code:
Here's a gif of how the program should work when completed:

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:
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
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
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.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
Now go to "Form1.vb", and add a new button. On the Button1_Click event, add this code:
Code: Select all
Finally, go into the Form1_Load event, and add this code:ShellMenu.Register("txtfile", "CustomIntegrationTest", "Explorer integration test App", Application.ExecutablePath & " ""%1""")
Code: Select all
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.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
Here's a gif of how the program should work when completed:

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
Our Button1 just acts to register our contextmenu for all text files. This shouldn't really need explaining much.ShellMenu.Register("jpegfile", "CustomIntegrationTest", "Explorer integration test App", Application.ExecutablePath & " ""%1""")
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!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
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]()
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!

:lol: i have used this function in my old apps
a few years back.
but nice tut
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
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
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
I'm glad you'll liked this.
More tutorials coming this month guys!
More tutorials coming this month guys!

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!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
Very nice tutorial Skillful!
You really do live up to your name.
You really do live up to your name.
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?
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.
so we could upload any program i context menu, and how to remove it without problems.
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 ?
button1 - add to context menu.
button2 - remove from context menu
Add with icons.
how about that ?
9 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023