How can I write a program that ads an item on windows right click menu?
I've searched for articles in this topic but no result...
Printable View
How can I write a program that ads an item on windows right click menu?
I've searched for articles in this topic but no result...
I can't see how I can manipulate the "Windows-explorer-right-click-menu" with that...
Maybe I misunderstood. Do you want to modify a right-click pop-up menu? The Windows Explorer menu? Could you please be a little more specific?
The windows explorer menu.
(When I right-click some files in windows explorer I want these files as input to my program that shoud invoke when my own option in the menu is choosen).
You do that by adding a few registry entries. First you associate your file extention with a key. Let's say you have a .tst file. You have to create a key in HKEY_CLASSES_ROOT named ".tst" with a value of "TstFile". Then create a "TstFile" key and, if you want to associate the icon of your application with that file type (so their icon will be the icon of your application), create a subkey named "DefaultIcon" inside "TstFile" with the default value being your executable (full-path and name).
To associate the open command with your application add another subkey inside "TstFile" named "shell". An "open" subkey inside "shell" and a "command" subkey inside "open". Set the default value of the "command" subkey to your executable (full path and name) and add a %1 at the end, and quote both. For example "C:\program files\myapp\myapp.exe" "%1".
Now when you double click on a .tst file or right-click and select open, it will start your application, passing it as a parameter the file.
You can do all of this programatically using the following code:
If you want another command instead of open, create another subkey and give it a name so that Explorer knows what to show in the menu:Code:using Microsoft.Win32;
...
Registry.SetValue("HKEY_CLASSES_ROOT\\.tst", "", "TstFile");
Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\DefaultIcon", "", Application.ExecutablePath); // this is the full path and file name of this application - you might want to put another path and name here
Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\shell\\open\\command", "", "\"" + Application.ExecutablePath + "\" \"%1\"");
This will behave like open but will have a different name. You can pass any parameters to your application in the above string.Code:Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\shell\\mycmd", "", "My Command"); // Explorer will show "My Command" on the menu
Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\shell\\mycmd\\command", "", "\"" + Application.ExecutablePath + "\" \"%1\"");
Now, inside your application, check if the Main method accepts arguments.
If it looks like static void Main() change it to static void Main(string[] args).
The args array will contain parameters passed to your application, in this case, the full path and file name of the file a user opened from explorer.
I'm sorry if this sounds a bit confusing and don't hesitate to ask for clarifications :)
Perfectt!
:thumb: