2

I am trying to create a desktop shortcut from vb.net code on a Windows 7 box (64 bit). The following code works on XP, but when run on Win7 I just get a message stating the App has stopped working:

Imports IWshRuntimeLibrary

Dim WshShell As WshShellClass = New WshShellClass

            Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut

            ' The shortcut will be created on the desktop
            'Win 7 
            MyShortcut = CType(WshShell.CreateShortcut("C:\Users\Public\Desktop\iexplore.lnk"), IWshRuntimeLibrary.IWshShortcut)

            'MyShortcut = CType(WshShell.CreateShortcut("C:\Documents and Settings\All Users\Desktop\iexplore.lnk"), IWshRuntimeLibrary.IWshShortcut)

            MyShortcut.TargetPath = "C:\Program Files\Internet Explorer\iexplore.exe" 'Specify target app full path
            MyShortcut.Description = "IE"

            MyShortcut.Save()

Any thoughts or better ways to create a shorcut from code on a Win7 box?

Matt
  • 2,078
  • 2
  • 27
  • 40

2 Answers2

2

Windows 7 64-bit here. Compiled this as 32-bit and it worked:

Imports IWshRuntimeLibrary

Module Module1

    Sub Main()
        Dim WshShell As WshShell = New WshShell

        Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut

        MyShortcut = CType(WshShell.CreateShortcut("C:\Users\Public\Desktop\Dah Browser.lnk"), IWshRuntimeLibrary.IWshShortcut)
        MyShortcut.TargetPath = "C:\Program Files\Internet Explorer\iexplore.exe" 'Specify target app full path
        MyShortcut.Description = "IE"

        MyShortcut.Save()
    End Sub

End Module

Note: I am running as admin with UAC turned off.

Also notice I changed WshShellClass to WshShell

David Crowell
  • 3,711
  • 21
  • 28
  • Oh, and I haven't done VB in years. Thanks for the mental exercise. :) – David Crowell Jun 14 '10 at 15:08
  • The question is asking how to do this on 64 bit Windows. That is not a 64 bit solution. It is a 32 bit solution. The code shown will not compile in a 64 bit application. – John Cruz Oct 27 '12 at 19:17
0

What privileges is your app running under? I believe it will need admin credentials to do what you are looking for.

Walter
  • 2,540
  • 2
  • 30
  • 38