Click to See Complete Forum and Search --> : Windows right click menu...


d00_ape
January 26th, 2008, 04:04 AM
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...

riscutiavlad
January 26th, 2008, 04:33 AM
http://www.codeguru.com/Csharp/Csharp/cs_misc/userinterface/article.php/c9327/

d00_ape
January 29th, 2008, 09:28 AM
I can't see how I can manipulate the "Windows-explorer-right-click-menu" with that...

riscutiavlad
January 30th, 2008, 02:09 AM
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?

d00_ape
January 30th, 2008, 06:24 AM
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).

riscutiavlad
January 31st, 2008, 05:57 AM
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:

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\"");

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:

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\"");

This will behave like open but will have a different name. You can pass any parameters to your application in the above string.

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 :)

d00_ape
February 1st, 2008, 08:06 AM
Perfectt!
:thumb: