Lanciare applicazione da menu contestuale di explorer

sabato 10 gennaio 2015 - 16.09
Tag Elenco Tags  VB.NET

hantrax Profilo | Newbie

Ciao, volevo scrivere un applicazione che funzioni in questo modo:
Da esplora risorse seleziono un file e con il tasto destro lancio un programma che fa alcune operazioni con il file. tipo quello che fa winrar
L'ideale sarebbe che nel menu contestuale aparisse il risultato della mia procedura.

Qualche esempio?

grazie

Stefano

0v3rCl0ck Profilo | Guru

Ciao Stefano, non ho una grande conoscenza dei context menu item di windows explorer, ma non sembra un compito tanto difficile con una piccola libreria open-source che ho trovato in rete:

http://github.com/dwmkerr/sharpshell

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

http://www.nuget.org/packages/SharpShell/

facile come estendere una classe:

/// <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...", Image = Properties.Resources.CountLines }; // 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 ouput. MessageBox.Show(builder.ToString()); } }


Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic

hantrax Profilo | Newbie

Grazie..la vedo subito!!

Stefano

hantrax Profilo | Newbie

Ciao, ho provato a convertire in vb.net, ma ricevo un error su questa parte: in particolare su Dim menu As var, dice che var non è un tipo definito

'/ <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 Overrides Function CreateMenu() As ContextMenuStrip
' Create the menu strip.
Dim menu As var = New ContextMenuStrip()

' Create a 'count lines' item.
var.itemCountLines = New ToolStripMenuItem
{
System.Text = "Count Lines...",
Image = Properties.Resources.CountLines
}


' 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
End Function

0v3rCl0ck Profilo | Guru

ricordati di usare il tag [code ].....[ /code] per postare il codice.

prova così (convertito con il servizio di telerik http://converter.telerik.com/):

Il codice sorgente non è stato renderizzato qui
perchè non c'è sufficiente spazio.
Clicca qui per visualizzarlo in una nuova finestra



Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic

hantrax Profilo | Newbie

]Ciao
Scusa se ti rompo, ma ho ancora un problema su questa parte:
in particolare sull'istruzione
itemCountLines.Click += Function(sender, args) CountLines()
mi dice che itemCountLines.Click non è dichiarato e non è accessibile
in particolare su
''' <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 Overrides Function CreateMenu() As ContextMenuStrip ' Create the menu strip. Dim menu = New ContextMenuStrip() ' Create a 'count lines' item. Dim itemCountLines = New ToolStripMenuItem() With { _ Key .Text = "Count Lines...", _ Key .Image = Properties.Resources.CountLines _ } ' When we click, we'll count the lines. itemCountLines.Click += Function(sender, args) CountLines() ' Add the item to the context menu. menu.Items.Add(itemCountLines) ' Return the menu. Return menu End Function

0v3rCl0ck Profilo | Guru

Ti ho allegato l'esempio convertito in vb.net, l'ho scritto direttamente in un progetto .net seguendo passo passo queste istruzioni: http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

Per provare la shell, installarla, ecc... SharpShell ha uno strumento davvero ottimo per fare tutto questo e lo puoi scaricare direttamente dal repo: http://github.com/dwmkerr/sharpshell/releases (download diretto: http://github.com/dwmkerr/sharpshell/releases/download/v2.2/ServerManager.zip)

fammi sapere se così riesci a fare qualcosa di più


Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic

hantrax Profilo | Newbie

Grazie laprovo e ti faccio sapere

Stefano

hantrax Profilo | Newbie

Ciao, scusa se è passato un po' di tempo ma il lavoro mi ha bloccato...

Sto provando ad usare il server manager, ma quando trascino la dll appare questo:

1920x1200 226Kb


Devo settare qualcosa sul server manager?

0v3rCl0ck Profilo | Guru

quale dll la mia?


Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic

0v3rCl0ck Profilo | Guru

stai usando questa versione?


884x547 30Kb


fai attenzione di compilare la dll con la stessa versione del server 2.2 e viceversa.

il packages nuget per l'ultima versione di server deve essere questo:

<package id="SharpShell" version="2.2.0.0" targetFramework="net45" />



Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic

hantrax Profilo | Newbie

Ciao
la dll è quella che mi hai mandato..
la versione del server è la stessa, seguo questi passi:

Ho scaricato il server manager
lo apro
trascino la dll compilata
ma non funziona,

0v3rCl0ck Profilo | Guru

hai provato ad eseguire l'applicazione come amministratore?

e ad aprire la dll da File -> Load Server? selezionando la dll "CountLinesExtension.dll"

poi selezioni il "server" CountLinesExtension e clicchi su "Test Server in Test Shell" così da provare il menu contestuale su un file per contarne le righe.


Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic

hantrax Profilo | Newbie

Ciao...finalmente ci sono riuscito!!

Grazie

0v3rCl0ck Profilo | Guru

ok bene :)


Michael Denny | Microsoft Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.net
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5