Creating a Desktop Shortcut

Christopherx

Well-known member
Joined
Jul 4, 2010
Messages
58
Programming Experience
Beginner
How do I create A Desktop Shortcut ? Is there any particular command that will do it for me ? :)
 
Add 'Interop.IWshRuntimeLibrary.dll' to your project and import it's namespace too. Add this code:
VB.NET:
    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
Then just call it passing a directory path and a shortcut title (the text that shows in windows):
VB.NET:
CreateShortCut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "AwesomeApp.lnk"), "My Awesome app")
 
Noticed Small Syntax Error in Your code

    
            Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName, IWshRuntimeLibrary.IWshShortcut)

i noticed a small Syntax error, the line above is missing a closing ")" after the word FileName and should be the following instead:
Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName), IWshRuntimeLibrary.IWshShortcut)
 
i noticed a small Syntax error, the line above is missing a closing ")" after the word FileName and should be the following instead:
Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName), IWshRuntimeLibrary.IWshShortcut)
Good catch, I've fixed my post.
 
Back
Top