VB - Create a desktop shortcut

By , 12/28/2012
(0 ratings)
Create a desktop shortcut.

Author: Volker Steitz, translation by Michael List
Function CreateShortCut(ByVal ShortCutTarget As String, _
                    ByVal ShortcutKey As String, _
                    ByVal ShortCutDescription As String) As Boolean

        ' Create the Shell Object of Windows Script Host.
        Dim WSHShell As Object = CreateObject("WScript.Shell")
        Dim DesktopDir As String = CType(WSHShell.SpecialFolders.Item("Desktop"), String)
        Dim Shortcut As Object

        'Shortcuts use the filetype *.lnk (link).
        Shortcut = WSHShell.CreateShortcut(DesktopDir & "\VS-SUPPORT.lnk")

        'setting properties of the shortcut
        '    Shortcut.TargetPath        = target
        '    Shortcut.HotKey            = shortcut key(s) to open the shortcut

        'Create ShortCut

        Shortcut.TargetPath = ShortCutTarget
        Shortcut.WindowStyle = 2
        Shortcut.Hotkey = ShortcutKey
        Shortcut.Description = ShortCutDescription
        Shortcut.WorkingDirectory = DesktopDir

        'Assign an Icon.
        'Either of the application, out of an exe or a dll-file
        'Attention: Begin to count at 0

        Shortcut.IconLocation = "explorer.exe, 0"

        ' Save the shortcut.

        MessageBox.Show("The ShortCut was created on your Desktop successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Return True
    Catch ex As Exception
        Return False
        MessageBox.Show("The ShortCut unfortunately could not be created on your Desktop." & vbCrLf & _
                        ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    CreateShortCut("http://www.vs-support.com", "CTRL+SHIFT+S", "VS-SUPPORT")
End Sub
Tagged with Shortcut, link, desktop, file.



Log in, to comment!