Windows Programming Custom Desktop File/Folder context menu extensions Using ShellSharp


Example

When you need multi level menus, with multiple parameters SharpShell comes to rescue. https://github.com/dwmkerr/sharpshell has umpteen number of examples and it works perfect even for single level to multi level custom context menus.

Key thing is to create class with attributes [ComVisible(true)] and [COMServerAssociation(AssociationType.AllFiles)] and inheriting class fromSharpContextMenu which implements CanShowMenu and CreateMenu functions and you need to register the assembly via regasm tool or ServerRegistrationManager that Sharpshell creator recommends

[ComVisible(true)]
[COMServerAssociation(AssociationType.AllFiles)]
public class AdvancedContextMenu : SharpContextMenu
{
    
    protected override bool CanShowMenu()
    {
        //  We can show the item only for a single selection.
    }
    protected override ContextMenuStrip CreateMenu()
    {
        //  Create the menu strip.
        var menu = new ContextMenuStrip();
        ... add any level of ToolStripMenuItems and add them to menu
        return menu
    }
}

More details can be obtained at https://github.com/dwmkerr and http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus