Adding Items to Context Menus (C#)
C# UncategorizedPublished April 15, 2011 at 11:36 am 1 CommentA friend and I like to quickly share images and other files with one another. We’ve used various methods in the past, but eventually we started to dislike each of them. So I decided, why not add an “Upload” to the context menu in windows explorer?
After searching around, I wanted to use the easiest method possible. So I’m going to show how it is done with the use of regedit. Afterward I’ll toss in some C# code to top it all off.
The location you want to modify depends on if you want the context menu to show up on files or folders. Also, I haven’t figured out how to limit the usage to certain types of files, but I’m going to look into it.
Here’s how it is done with regular files (like how I have it above):
First locate this in regedit: HKEY_CLASSES_ROOT\*\shell
Here you can specify additional options for your file context menus. By creating a key inside the shell you can specify the text and command for that option.
The value of the key that you create will be the text that is displayed in the menu. The subkey called command is the file that will be ran when this option is selected.
Below shows an example of how you should specify the application you wish to run. I would suggest setting your registry during program installation so that it syncs with where the user put it. For me I used my Visual Studio bin just to test it out with my uploader application.
The %1 you see in the application path specifies the parameter passed into the context menu. When you right click a file and choose “Upload” the %1 gets replaced with the file path. Placing it after the application’s path will then pass that as a parameter to the application. In the case of my uploader, I read this parameter and upload the file.
When adding context menus to folders, you do the same steps as above, but in a different place. Folder related context menus can be found at:
HKEY_CLASSES_ROOT\Folders\shell
Finally, let’s look at how we can use this in C#. Please note that I am only displaying C# code, but this same technique can be done in any language that has access to the registry. Just add in the keys with your technique of choice, and it’ll update the context menus. With that in mind, let’s take a look how this would be done in C#.
try
{
RegistryKey RegKey =
Registry.ClassesRoot.CreateSubKey("*\\shell\\OptionName");
RegKey.SetValue("", "Option Description");
RegKey =
Registry.ClassesRoot.CreateSubKey("*\\shell\\OptionName\\command");
RegKey.SetValue("", "%somepath%\\myapplication.exe %1");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}





hi
your code its working fine .
but its possible to change the path of the upload command;
i mean
when i right click on image its show the upload option and click on upload its should be save on the given path it’s it possible.
thanks and regards