5

I want to create desktop shortcut for exe file through program. I do not want to use installer to do this. Can a piece of code in program do this?How?

vaichidrewar
  • 9,251
  • 18
  • 72
  • 86

2 Answers2

12

Chasler has answered that question a couple of years ago here on SO.

Add a reference to the Windows Script Host Object Model

Imports IWshRuntimeLibrary

Private Sub CreateShortCut(ByVal FileName As String, ByVal Title As String)
    Try
        Dim WshShell As New WshShell
        ' short cut files have a .lnk extension
        Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName, IWshRuntimeLibrary.IWshShortcut)

        ' set the shortcut properties
        With shortCut
            .TargetPath = Application.ExecutablePath
            .WindowStyle = 1I
            .Description = Title
            .WorkingDirectory = Application.StartupPath
            ' the next line gets the first Icon from the executing program
            .IconLocation = Application.ExecutablePath & ", 0"
            .Arguments = String.Empty
            .Save() ' save the shortcut file
        End With
    Catch ex As System.Exception
        MessageBox.Show("Could not create the shortcut" & Environment.NewLine & ex.Message, g_strAppTitleVersion, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

(Source)

Community
  • 1
  • 1
Rhapsody
  • 6,017
  • 2
  • 31
  • 49
  • Let's note for anyone's consideration that it's a proprietary SW: "Developed by Sage Software, Interop.IWshRuntimeLibrary.dll is a shared dynamic link library file. It belongs to the contact management software Act! 2009" - by https://softwaretested.com/files/what-is-interop-iwshruntimelibrary-dll – Oak_3260548 Feb 14 '23 at 10:09
4

This code works very well

 Private Function CreateShortCut(ByVal TargetName As String, ByVal ShortCutPath As String, ByVal ShortCutName As String) As Boolean
    Dim oShell As Object
    Dim oLink As Object
    'you don’t need to import anything in the project reference to create the Shell Object

    Try

        oShell = CreateObject("WScript.Shell")
        oLink = oShell.CreateShortcut(ShortCutPath & "\" & ShortCutName & ".lnk")

        oLink.TargetPath = TargetName
        oLink.WindowStyle = 1
        oLink.Save()
    Catch ex As Exception

    End Try

End Function

Credits