| Add a shortcut to the right click menu (C#) |
|
Question I'd like to add a program I've made to the Windows right click menu. However, My Googling for how to do this with the Shell have turned up little thus far. Answer Don't. Remulak's right about the reason that you shouldn't do this with .NET. Because this method works only for Windows 95 / Windows 98 (not on XP, Vista, x64 - 64-bit Windows), to add items to Windows Explorer Shell context menu you should use, according to Microsoft guidelines, appropriate .Net component - Windows Explorer Shell Context Menu. What you may also do is to write a launcher program in C/C++ that invokes your program with the arguments you want. Adding a "dumb" context sensitive shortcut to the right-click menu is ok as its simply a registry entry & wont invoke the underlying application. Could you elaborate on this (why not to use C#, why a launcher would help, and what a launcher would look like)? I was going to write a C# program which is invoked via shell extensions, but I'll do it this way if that would be bad practice. Sure. You write a context menu item so that you can invoke your program (or an operation of your program) on a specific file system object, and what the context menu item gives you is the ID file system object that was clicked on. For instance, when you right-click on a text file and select "Notepad" in the "Open With" menu, Explorer raises "%systemroot%\notepad.exe %1" where %1 is the path of the text file you clicked on. Write a launcher program that takes the arguments that Explorer gives it and passes those directly to your C# program. You shouldn't use C# to write shell extensions because of a limitation with current versions of the .NET runtime. Namely, that only one version of the runtime can be loaded into a process at a time.
Thanks for the info, guys! What I ended up doing was just going through the folder options File Types dialogue in explorer and adding the context menu item that way for the various extentions it can accept.
Well. There's more than one way to get an entry into a context menu. First of all, which context menu? A specific file type? All files? Just directories? By your description there, it sounds like a few file types. This is possible to do without a shell extension, by adding a verb to the directory or extension, which is actually what you're doing with file types. code:Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Classes\Directory] [HKEY_CURRENT_USER\Software\Classes\Directory\shell] [HKEY_CURRENT_USER\Software\Classes\Directory\shell\OpenWithProgram] @="Convert images to text" [HKEY_CURRENT_USER\Software\Classes\Directory\shell\OpenWithProgram\command] @="\"c:\\my program\\ocr.exe\" \"%1\""For file types, verbs are added to that extension's progid. Here is an example that adds a command to the context menu of .jpg files. On my machine, .jpg files have a progid of "jpegfile", so that's where the command goes. You can look up an extension's progid by checking the default value of HKEY_CURRENT_USER\Software\Classes\<the extension> key. code:Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Classes\jpegfile\shell\OpenWithProgram] @="Convert images to text" [HKEY_CURRENT_USER\Software\Classes\jpegfile\shell\OpenWithProgram\command] @="\"c:\\my program\\ocr.exe\" \"%1\""There's more places to put verbs, too, but that should get you going. Note that you can easily manipulate the registry in code, and you don't need administrative permissions to write to HKCU, either, which is great. E2: On the topic of shell extensions, it's annoying that I can't write them in C#. I've been thinking, it should be possible to write an unmanaged shell extension that, when loaded, loads a managed library in it's own address space, and passes the calls right to the managed library. This would be possible, right? This I find incredibly interesting. I've always found it odd that Microsoft wrote an entirely new language and framework and then never actually used it themselves. Don't misunderstand, Microsoft is using .NET to develop applications more and more, just not for anything that installs as part of a standard Windows installation -- outside of Windows Media Center, which is kinda an odd duck out. new question: I would like to make a multi level item on the menu- an item with a sub menu. How would I go about doing this? Most searching I do refers to a IContextMenu object for .NET This is going to require a shell extension, which means no .Net.
|