I was trying to develop a shell extension via SharpShell in windows 11
Firstly, following the demo “CountLines” in this guide: https://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus,
I copied the code and build it in Release mode, everything works fine by now.
Then, I tried several methods to deploy the generated dll, such as regasm, srm or ServerManager, each method works. However, the Context Menu for txt files doesn’t have a “CountLines” item. By the way, it works fine in ServerManager.exe test shell.
Then, I used ShellexView to check my extension, it do existed. But its file path shows:
“C:WindowsSystem32D:/my/custom/path/shellext.dll”
Why did it happen?
Here comes my code and some results:
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using SharpShell;
using SharpShell.SharpContextMenu;
using SharpShell.Attributes;
using System.Windows.Forms;
/// <summary>
/// The CountLinesExtensions is an example shell context menu extension,
/// implemented with SharpShell. It adds the command 'Count Lines' to text
/// files.
/// </summary>
[ComVisible(true)]
[COMServerAssociation(AssociationType.ClassOfExtension, ".txt")]
public class CountLinesExtension : SharpContextMenu
{
/// <summary>
/// Determines whether this instance can a shell
/// context show menu, given the specified selected file list
/// </summary>
/// <returns>
/// <c>true</c> if this instance should show a shell context
/// menu for the specified file list; otherwise, <c>false</c>
/// </returns>
protected override bool CanShowMenu()
{
// We always show the menu
return true;
}
/// <summary>
/// Creates the context menu. This can be a single menu item or a tree of them.
/// </summary>
/// <returns>
/// The context menu for the shell context menu.
/// </returns>
protected override ContextMenuStrip CreateMenu()
{
// Create the menu strip
var menu = new ContextMenuStrip();
// Create a 'count lines' item
var itemCountLines = new ToolStripMenuItem
{
Text = "Count Lines...",
};
// 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;
}
/// <summary>
/// Counts the lines in the selected files
/// </summary>
private void CountLines()
{
// 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());
}
}
in ServerManager.exe test shell:
enter image description here
Install shellext server in ServerManager.exe:
enter image description here
Weird filename in shellexview:
enter image description here
However, in registry, its path seems right?
enter image description here
I have tried to use srm or regasm to register the dll, both of them show the same result
Mercu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.