Create a shortcut at run time

zekeman

Well-known member
Joined
May 23, 2006
Messages
224
Programming Experience
10+
I'm trying to get my VB.Net program to create one shortcut on the

desktop not during install but while running. I found this code at site:

http://www.tek-tips.com/faqs.cfm?fid=6127

VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] IWshRuntimeLibrary 
[/SIZE]
Private Function CreateShortCut(ByVal shortcutName As String, ByVal creationDir As String, ByVal targetFullpath As String, ByVal workingDir As String, ByVal iconFile As String, ByVal iconNumber As Integer) As Boolean
  Try
    If Not IO.Directory.Exists(creationDir) Then
      Dim retVal As DialogResult = MsgBox(creationdir & " does not exist. Do you wish to create it?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
      If retVal = DialogResult.Yes
        IO.Directory.CreateDirectory(creationDir)
      Else
        Return False
      End If
    End If
 
    Dim shortCut As IWshRuntimeLibrary.IWshShortcut
    shortCut = CType([COLOR=red]wShell[/COLOR].CreateShortcut(creationDir & "\" & shortcutName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)
    shortCut.TargetPath = targetFullpath
    shortCut.WindowStyle = 1
    shortCut.Description = shortcutName
    shortCut.WorkingDirectory = workingDir
    shortCut.IconLocation = iconFile & ", " & iconNumber
    shortCut.Save()
    Return True
  Catch ex As System.Exception
    Return False
  End Try
End Function

I also added a reference to a com named:
''Windows Script Host Object Model''.

My problem is that the 'wShell' shown in red in the code above is
underlined as an error and says:

'Name wShell is not declared'.

How do I declare it?
 
WshShell is an interface in the IWshRuntimeLibrary, you need to create an object of it first

VB.NET:
Dim wShell As WshShell = New WshShell

Not sure why that example doesn't?
 
Are you sure Jmcilhinney? it's only i tried that and it didn't work. I did make a small typo in my original post though. everything i tried needed me to create an instance of the WshShellClass. Which is what i ment to say in my original post. So it should have been...

VB.NET:
Dim WshShell as new WshShellClass
 
VB.NET:
Private Function CreateShortCut(ByVal shortcutName As String, ByVal creationDir As String, ByVal targetFullpath As String, ByVal workingDir As String, ByVal iconFile As String, ByVal iconNumber As Integer) As Boolean
   [SIZE=2][COLOR=#0000ff]
   Dim[/COLOR][/SIZE][SIZE=2] wShell [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] WshShell = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] WshShell[/SIZE]
 
[SIZE=2] ...[/SIZE]
 
[SIZE=2]end sub[/SIZE]

I added the:

Dim wShell as WshShell = New WshSell

and all seems to work OK

 
Back
Top