none
Create Desktop Shortcut RRS feed

  • Question

  • I am using the below code I found but can not figure out how to enter the value to the shortcut.

    Where do I specify the shortcut location, I have tried FileName but it generated an HRESULT error.

    I simply want to create a desktop shortcut to C:\Windows\notepad.exe (Its actually a different file but this is a common file)

    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

    Tuesday, February 4, 2020 2:35 PM

All replies

  • For example, with Notepad =>

    Dim wshShell As WshShell = New WshShell()
    Dim sShortcut As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Notepad.lnk"
    Dim shortcut As IWshShortcut = CType(wshShell.CreateShortcut(sShortcut), IWshShortcut)
    shortcut.Description = "Notepad Shortcut"
    shortcut.Hotkey = "Ctrl+Shift+N"
    shortcut.IconLocation = "%SystemRoot%\System32\shell32.dll,2"
    shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) & "\notepad.exe"
    shortcut.Save()

    Tuesday, February 4, 2020 3:04 PM
  • Set the TargetPath property to the fully qualified path of your executable, (e.g., "C:\Windows\Notepad.exe"
    Tuesday, February 4, 2020 3:05 PM
  • Hi Ryan Lashway,

    How’s it going? Do you have any updates about the  issue? Please reply to let us know if this issue has been resolved. If so, I can mark this thread as answer.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 13, 2020 9:13 AM
    Moderator