Question

I've found a very nice tutorial and i am trying to understand something that is not in this tutorial (because the tut itself works fine) http://www.codeproject.com/Articles/9163/File-Rating-a-practical-example-of-shell-extension

When you look at applications like WinRar, TortoiseSVN, Antivirus-apps and many more, there is an icon next to the Shell Extension Item.

I would like to know how this is done. (Programmatically with C#)

Adding a separator works, adding a submenu works and click+action also works, but i'm struggling with the icon. This cannot be so hard. Can somebody help me?

And please don't say that Microsoft doesn't longer support this in .NET 4.0, because it is not guaranteed and therefore they don't supply samplecode. If all those other apps can do it, then it is possible.

Please supply me some sample code, some tutorials or maybe even a working piece of code.

Was it helpful?

Solution

Please have a look at the following article, it uses .NET 4.0 it to create Windows Shell Extensions using the SharpShell nuget package.

NET Shell Extensions - Shell Context Menus

Using this library, you can set the image directly while creating the contextmenustrip as shown below

protected override ContextMenuStrip CreateMenu()
{
    //  Create the menu strip.
    var menu = new ContextMenuStrip();

    //  Create a 'count lines' item.
    var itemCountLines = new ToolStripMenuItem
    {
        Text = "Count Lines...",
        Image = Properties.Resources.CountLines
    };

    //  When we click, we'll count the lines.
    itemCountLines.Click += (sender, args) => CountLines();

    //  Add the item to the context menu.
    menu.Items.Add(itemCountLines);

    //  Return the menu.
    return menu;
}

OTHER TIPS

You only have to add to the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Classes*\shellex\ContextMenuHandlers and here is the code:

 string TimeStamp = DateTime.Now.ToString("dd-MM-yyyy");

string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\*\\shellex\\ContextMenuHandlers\\Winrar";
string valueName = "MyWinrar";
Microsoft.Win32.Registry.SetValue(key, valueName, HERE WHAT YOU WANT TO START, Microsoft.Win32.RegistryValueKind.String);

i hope it works for you!

All the apps you listed use COM and unmanaged code to create overlay icon handlers. There is even a special project TortoiseOverlays that provides a common library for drawing icons for TortoiceCSV, TortoiseSVN and TortoiseGIT. You can take a look at it's source code to find out how it is done. If you want to draw similar icons, you should probably just reuse it.

Using .Net for this type of extensions is not recommended, because when multiple extensions, built against different .Net versions would attempt to load in explorer process, they will crash the explorer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top