[vb.net] creare collegamento di un programma

giovedì 15 aprile 2010 - 12.01

marco444 Profilo | Newbie

Ciao a tutti,
come si crea tramite codice il collegamento .lnk di un programma?

kataklisma Profilo | Senior Member

>Ciao a tutti,
Ciao!

>come si crea tramite codice il collegamento .lnk di un programma?
Ecco uno snippet che uso la libreria STKIT432.Dll :

Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal _ lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal _ lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long ' 'code to create the shortcut ' Dim lReturn As Long 'Add to Desktop lReturn = fCreateShellLink("..\..\Desktop", _ "Shortcut to Calculator", "c:\Windows\calc.exe", "") 'Add to Program Menu Group lReturn = fCreateShellLink("", "Shortcut to Calculator", _ "c:\Windows\calc.exe", "") 'Add to Startup Group 'Note that on Windows NT, the shortcut will not actually appear 'in the Startup group until your next reboot. 'lReturn = fCreateShellLink("\Startup", "Shortcut to Calculator", _ '"c:\Winnt\system32\calc.exe", "")

Oppure prova a dare un'occhiata qui, è piu semplice :

http://www.geekpedia.com/tutorial125_Create-shortcuts-with-a-.NET-application.html

;)

------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/

marco444 Profilo | Newbie

ho provato così:
Public Class Form1
Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal _
lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal _
lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'
'code to create the shortcut
'
Dim lReturn As Long

'Add to Desktop
lReturn = fCreateShellLink("c:\", "Shortcut to Calculator", "c:\Windows\programma.exe", "")

'Add to Program Menu Group
lReturn = fCreateShellLink("", "Shortcut to Calculator", _
"c:\Windows\calc.exe", "")

'Add to Startup Group

'Note that on Windows NT, the shortcut will not actually appear
'in the Startup group until your next reboot.
'lReturn = fCreateShellLink("\Startup", "Shortcut to Calculator", _
'"c:\Winnt\system32\calc.exe", "")
End Sub
End Class

ma mi da un errore:
System.DllNotFoundException non è stata gestita
Message="Impossibile caricare la DLL 'STKIT432.DLL': Impossibile trovare il modulo specificato. (Eccezione da HRESULT: 0x8007007E)."
Source="WindowsApplication1"
TypeName=""
StackTrace:
in WindowsApplication1.Form1.fCreateShellLink(String& lpstrFolderName, String& lpstrLinkName, String& lpstrLinkPath, String& lpstrLinkArgs)
in WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:riga 14
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
in WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:riga 81
in System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()
InnerException:

kataklisma Profilo | Senior Member

>ho provato così:
>Public Class Form1
>Private Declare Function fCreateShellLink Lib "STKIT432.DLL"
>(ByVal _
>lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal
>_
>lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long
>
>Private Sub Button1_Click(ByVal sender As System.Object, ByVal
>e As System.EventArgs) Handles Button1.Click
>
> '
> 'code to create the shortcut
> '
> Dim lReturn As Long
>
> 'Add to Desktop
>lReturn = fCreateShellLink("c:\", "Shortcut to Calculator", "c:\Windows\programma.exe",
>"")
>
> 'Add to Program Menu Group
>lReturn = fCreateShellLink("", "Shortcut to Calculator", _
> "c:\Windows\calc.exe", "")
>
> 'Add to Startup Group
>
>'Note that on Windows NT, the shortcut will not actually appear
> 'in the Startup group until your next reboot.
>'lReturn = fCreateShellLink("\Startup", "Shortcut to Calculator",
>_
> '"c:\Winnt\system32\calc.exe", "")
> End Sub
>End Class
>
>ma mi da un errore:

System.DllNotFoundException non è stata gestita

Piu esplicito di cosi...manca la dll, comuqnue sia nel mio primo post ti ho inserito un link, dagli un'occhiata ti facilita un po le cose ;)

------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/

marco444 Profilo | Newbie

in realtà fino ad un certo punto, perchè non mi fa scaricare l'esempio e in piu il codice è per C

kataklisma Profilo | Senior Member

>in realtà fino ad un certo punto, perchè non mi fa scaricare
>l'esempio e in piu il codice è per C

Io riesco a scaricarlo vai qui :

http://www.geekpedia.com/pics/CreatingShortcuts/CreatingShortcuts.zip

Per quanto riguarda il fatto che sia in C#, posso garantirti che la conversione è molto semplice da fare...

------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/

marco444 Profilo | Newbie

anche il progetto è in c, ma non riesco a convertirlo, non c'è un progetto esempio fatto in vb.net?

kataklisma Profilo | Senior Member

>anche il progetto è in c, ma non riesco a convertirlo, non c'è
>un progetto esempio fatto in vb.net?

Io programmo sostanzialmente in C# quindi la mia referenza sull'argomento era quella, comunque questo è un sorgente Vb.Net

http://www.tek-tips.com/faqs.cfm?fid=6127

Spero risolva il problema :)

------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/

marco444 Profilo | Newbie

scusatemi so che rompo però ho fatto questo:
Imports IWshRuntimeLibrary

Public Class Form1

Private Function CreateShortCut(ByVal shortcutName As String, ByVal creationDir As String, ByVal targetFullpath As String, ByVal workingDir As String, ByVal iconFile As String, ByVal iconNumber As Integer) As Boolean
Try
If Not IO.Directory.Exists(creationDir) Then
Dim retVal As DialogResult = MsgBox(creationdir & " does not exist. Do you wish to create it?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
If retVal = DialogResult.Yes Then
IO.Directory.CreateDirectory(creationDir)
Else
Return False
End If
End If

Dim shortCut As IWshRuntimeLibrary.IWshShortcut
shortCut = CType(wShell.CreateShortcut(creationDir & "\" & shortcutName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)
shortCut.TargetPath = targetFullpath
shortCut.WindowStyle = 1
shortCut.Description = shortcutName
shortCut.WorkingDirectory = workingDir
shortCut.IconLocation = iconFile & ", " & iconNumber
shortCut.Save()
Return True
Catch ex As System.Exception
Return False
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myBool = CreateShortCut("My Application", System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Application.ExecutablePath, Application.StartupPath, "myAppExtensions.dll", 4)
End Sub


End Class

e mybool e wshell dice che non è dichiarato.

kataklisma Profilo | Senior Member

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

Spero ti sia piu chiaro ora!
------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/

marco444 Profilo | Newbie

ma a te funziona?
perchè mi dice che WshShell non è difinito

kataklisma Profilo | Senior Member

>ma a te funziona?
>perchè mi dice che WshShell non è difinito

Certo che funziona

Sicuramente non hai aggiunto il riferimento a Windows Script Host Object Model.

Allora, click destro sul progetto, aggiungi riferimento, nella nuova finestra clicca sul Tab 'Com', cerca Windows Script Host Object Model e aggiungilo al progetto.

Successivamente importa la libreria IWshRuntimeLibrary in questo modo :

Imports IWshRuntimeLibrary

;)

------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/

marco444 Profilo | Newbie

fenomenale grazie mille funziona perfettamente,
una piccola domanda finale, con questo codice posso tirare ancora fuori l'exe da debug senza bisogno di dll?

kataklisma Profilo | Senior Member

>fenomenale grazie mille funziona perfettamente,
>una piccola domanda finale, con questo codice posso tirare ancora
>fuori l'exe da debug senza bisogno di dll?

Mi dispiace ma non ho capito cosa intendi dire...

------------------------------------------
Ignazio Catanzaro

http://blogs.dotnethell.it/swdev/
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-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5