2

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?

soulblazer
  • 1,178
  • 7
  • 20
  • 30

2 Answers2

3

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
Joe Uhren
  • 1,342
  • 1
  • 13
  • 27
2
 Imports Microsoft.Win32 and
 Imports IWshRuntimeLibrary

TO CREATE A SHORTCUT

Private Sub btnCreateShortcut_Click_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateShortcut.Click
    'To create Start shortcut in the windows Startup folder: 
    Dim WshShell As WshShell = New WshShell
    Dim SRT As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
    'Fixing the Shortcut Location instead of StartupScreenLocker
    'Name your Shortcut, Example \ScreenLocker.Ink
    Dim ShortcutPath As String = SRT & "\ScreenLocker.lnk"

    'Add shortcut.
    Dim Shortcut As IWshRuntimeLibrary.IWshShortcut
    Shortcut = CType(WshShell.CreateShortcut(ShortcutPath), IWshRuntimeLibrary.IWshShortcut)
    Shortcut.TargetPath = Application.ExecutablePath
    Shortcut.WorkingDirectory = Application.StartupPath
    Shortcut.Save()
End Sub

TO DELETE THE SHORTCUT

Private Sub btnDeleteShortcut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteShortcut.Click

    'To create Start shortcut in the windows Startup folder: 
    Dim Shortcut As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)

     'Name the Shortcut you want to delete, Example \ScreenLocker.Ink
    Dim ShortcutPath As String = Shortcut & "\ScreenLocker.lnk"

    'To delete the shortcut:
    If IO.File.Exists(ShortcutPath) Then
        IO.File.Delete(ShortcutPath)
    End If
End Sub