我在CodeProject上发现了这篇文章:http: //www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus
并且认为尝试一下会很好,但在F#中.所以我想出了以下代码:
open System
open System.IO
open System.Text
open System.Runtime.InteropServices
open System.Windows.Forms
open SharpShell
open SharpShell.Attributes
open SharpShell.SharpContextMenu
[<ComVisible(true)>]
[<COMServerAssociation(AssociationType.ClassOfExtension, ".txt")>]
type CountLinesExtension() =
inherit SharpContextMenu.SharpContextMenu()
let countLines =
let builder = new StringBuilder()
do
base.SelectedItemPaths |> Seq.iter (fun x -> builder.AppendLine(sprintf "%s - %d Lines" (Path.GetFileName(x)) (File.ReadAllLines(x).Length)) |> ignore )
MessageBox.Show(builder.ToString()) |> ignore
let createMenu =
let menu = new ContextMenuStrip()
let itemCountLines = new ToolStripMenuItem(Text = "Count Lines")
do
itemCountLines.Click.Add (fun _ -> countLines)
menu.Items.Add(itemCountLines) …Run Code Online (Sandbox Code Playgroud)