vb.netshortcutlnkstartup-folder

Create Shortcut in StartUp folder (VB.NET)


I have this code and it's giving troubles:

Imports IWshRuntimeLibrary
Imports Shell32

Public Sub CreateShortcutInStartUp(ByVal Descrip As String)
    Dim WshShell As WshShell = New WshShell()
    Dim ShortcutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
    Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(ShortcutPath &    
    Application.ProductName & ".lnk"), IWshShortcut)
    Shortcut.TargetPath = Application.ExecutablePath
    Shortcut.WorkingDirectory = Application.StartupPath
    Shortcut.Descripcion = Descrip
    Shortcut.Save()
End Sub

According to what I have read, this is how you create a shortcut in Startup. But, no matter how much I call this Sub, shortcut does not show up. I ALREADY look up to a lot of similar questions around S.O and various other sites.

I even tried to create the shortcut from other application and still doesn't show up as expected.

What am I missing?


Solution

  • You have two errors in your code:

    1) The path isn't being concatenated properly so change this:

    Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(ShortcutPath & Application.ProductName & ".lnk"), IWshShortcut)
    

    to this:

    Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(System.IO.Path.Combine(ShortcutPath, Application.ProductName) & ".lnk"), IWshShortcut)
    

    2) You spelled Description wrong so change:

    Shortcut.Descripcion = Descrip
    

    to this:

    Shortcut.Description = Descrip
    

    Here is the fixed subroutine:

    Public Sub CreateShortcutInStartUp(ByVal Descrip As String)
        Dim WshShell As WshShell = New WshShell()
        Dim ShortcutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
        Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(System.IO.Path.Combine(ShortcutPath, Application.ProductName) & ".lnk"), IWshShortcut)
        Shortcut.TargetPath = Application.ExecutablePath
        Shortcut.WorkingDirectory = Application.StartupPath
        Shortcut.Description = Descrip
        Shortcut.Save()
    End Sub