The .NET Shell extension framework called SharpShell is great; I've developed a right-click file Shell ContextMenu "quite easily" that works selecting both files and directories.
Now I would like to develop a Shell ContextMenu by righ-clicking on an empty space (that is, on the Desktop or on a white spot while I'm inside a folder). Is it possible do that still using SharpShell? Or do I need to move to a different solution?... and in 2nd case... what do you suggest?
Thanks
The two solutions presented below work, but in the meantime I have found that there is an easier solution that is actually already used in the samples that come with SharpShell.
See the
CopyDirectoryLocationHandlerclass as an example of a context menu handler that is registered for the directory background (and the desktop):If you want the handler to only handle clicks on the desktop background, use this code instead:
Old obsolete answer:
You can use SharpShell for this purpose without problem. There are two possible approaches:
or
Register the Shell Extension to handle the folder background yourself
Your shell extension is a COM server and as such is identified to the system via a GUID. This GUID is then used at places in the registry to register the COM extension for different purposes. When we manually want to register the extension for a purpose such as extending the context menu for folder backgrounds, it is best when our extension has a fixed GUID.
Currently your class looks like this:
When compiling, the compiler will automatically generate a GUID to use for that class. But we can specify a specific one to use like this:
Do not use the same GUID as shown here but create your own unique one in Visual Studio via Menu Tools > Create GUID. Use a different GUID for every shell extension you write.
Then recompile the dll and install and register it again (using regasm or the SharpShell Server Manager tool.
Then create a text file named "registry.reg" with the following content (use your own specific GUID). Instead of "MyContextMenuExtension" specify the name of your extension.
Install the "registry.reg" file by double clicking. Your extension should now be active when you open the context menu for a folder background or the Desktop.
Instead of using the *.reg file, you can also make the changes manually using registry editor or if you have an installer instruct the installer to make those registry changes.
Modify SharpShell to handle the registration of the extension for the folder background for you
Make the following changes to the SharpShell source code:
In the file
AssociationType.csadd a new enum value to theAssociationTypeenumeration:In the file
ServerRegistrationManager.csadd a new private string constant:Also in the file
ServerRegistrationManager.csin the methodCreateClassNamesForAssociationsin the big switch statement add a new case like this:Finally you only have to tell your own extension class to use this new enumeration value: