I found this article on CodeProject: http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus
and thought it would be nice to give it a try, but in F#. So I came up with the following code:
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) |> ignore
menu
override this.CanShowMenu() = true
override this.CreateMenu() = createMenu
However, I noticed that there is no support for signing an F# assembly in VS2012 (step 4. in the article). I learnt that if I want to do so, I need to create a key manually (typing "sn -k keyName.snk" into the command prompt) and then add a flag in "Project Properties -> Build -> Other Flags" (--keyfile:keyName.snk).
I still didn't manage to successfully run this. Moreover, using the author's application (in "Debugging the Shell Extension" section) I get an error that my assembly doesn't contain a COM server.
I believe I'm doing something wrong with the signing the component. Could you help me in running this ?
sn -v
(or is it-V
?) to verify your signing but bottom line is there will be an error somewhere saying authenticode signature error if that's the problem. And get ProcMon and DbgView on the case. If you get any more detail re exact error messages etc., no harm to add that detail here