Search code examples
c#sharpshell

Shell extension menu item doesn't appear in Win11/Win10


I'm using SharpShell.dll v2.7.2 and I carefully followed the instructions here:

https://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

I did change some very minor things such as class name and function name, but I left most functionality intact. It compiles fine and regasm was successful. But the new shell extension menu item doesn't appear in Windows10 or Windows11. Here is my final code:

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;


namespace AcmeFileSender
{
   [ComVisible(true)]
   [COMServerAssociation(AssociationType.ClassOfExtension, ".*")]
   public class UploadFileExtension : SharpContextMenu
   {
      protected override bool CanShowMenu()
      {
         //  We always show the menu
         return true;
      }

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

         //  Create an 'Upload file' item
         var itemCountLines = new ToolStripMenuItem
         {
            Text = "Upload file"
         };

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

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

         //  Return the menu
         return menu;
      }

      private void UploadFiles()
      {
         //  Builder for the output
         var builder = new StringBuilder();

         //  Go through each file.
         foreach(var filePath in SelectedItemPaths)
         {
            //  Count the lines
            builder.AppendLine(string.Format("{0} - {1} Lines", Path.GetFileName(filePath), File.ReadAllLines(filePath).Length));
         }

         //  Show the output
         MessageBox.Show(builder.ToString());
      }
   }
}

A few notes about the code above:

  1. I used .* instead of .txt, because the eventual purpose of this shell extension will be to upload files to my server with minimal mouse clicks, and I want it to apply to ALL files, not just .txt files.
  2. I removed the image reference beneath Text = "Upload file" because it wouldn't compile. The image icon didn't seem important so I removed it.

As you can see, most of the code is the same as the sample. In Win11, I'm using the classic/legacy context menu and it still doesn't work.

I have tried clearing all registry entries related to this shell extension twice, and re-ran regasm twice (always succeeds), with no luck in see the new shell menu item. Is a reboot required?

Here is my DLL loaded in Server Manager:

enter image description here

I see 2 potential problems:

  1. 64-bit servers shows not installed. Why? Is this a problem?
  2. Both 32-bit and 64-bit servers show as not registered. Definitely seems like a problem (and maybe THE problem), but I ran regasm against my DLL and received the message "Types registered successfully".

What am I doing wrong? How can I get Upload file to appear in the context menu in Win10/Win11 when I right-click any file inside windows explorer?


Solution

  • If you want the extension to apply to all files, explicitly use

    [COMServerAssociation(AssociationType.AllFiles)]
    

    Instead of

    [COMServerAssociation(AssociationType.ClassOfExtension, ".*")]
    

    See the SharpShell documentation on associations: https://github.com/dwmkerr/sharpshell/blob/main/docs/com-server-associations.md


    For distribution, make sure you compile the server as "Any CPU" so that it can be installed as both 32 and 64 bit. Also make sure it does not depend on some external assembly or dll which is 32 or 64bit only.


    The best way to install your server on a customer computer is to use the Server Registration Manager tool srm.exe as explained on https://www.codeproject.com/Articles/653780/NET-Shell-Extensions-Deploying-SharpShell-Servers