After the CLR in-process side-by-side feature is introduced in. NET 4, we can use C # To compile Windows Shell. We can find related examples In Microsoft's All-In-One Code Framework. There are also several articles about it In the yard:
- Windows Shell extension series Article 1-. NET 4 compiling Windows Shell context menu Extension
- Windows Shell extension Series 2-. NET 4 Add bitmap icons for extended Windows Shell context menu items
However, after studying these examples, we will find that although it is much easier to write Shell extensions in C # than to use C ++, it is still cumbersome and there are many things to be aware, an error occurs when you are not careful.
Today, we found a SharShell project on CodePlex, which allows you to quickly create Shell extensions, which is very convenient. For example, for a menu extension as follows:
You only need to use the following statements:
[ComVisible (true)]
[COMServerAssocation (AssociationType. ClassOfExtension, ". txt")]
Public
Class
CountLinesExtension: SharpContextMenu
{
Protected
Override
Bool CanShowMenu ()
{
Return
True;
}
Protected
Override ContextMenuStrip CreateMenu ()
{
Var menu = new ContextMenuStrip ();
Var itemCountLines = new ToolStripMenuItem
{
Text = "Count Lines ...",
Image = Properties. Resources. CountLines
};
ItemCountLines. Click + = (sender, args) => CountLines ();
Menu. Items. Add (itemCountLines );
Return menu;
}
Private
Void CountLines ()
{
Var builder = new
StringBuilder ();
Foreach (var filePath in SelectedFilePaths)
{
Builder. AppendLine (string. Format ("{0}-{1} Lines", Path. GetFileName (filePath), File. ReadAllLines (filePath). Length ));
}
MessageBox. Show (builder. ToString ());
}
}
For more information, see the Getting Started tutorial on CodeProject:. NET Shell Extensions-Shell Context Menus.