views:

59

answers:

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?

A: 

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

Walter
The account is an Administrator
Matt
that is not the same as running elevated. Unless it's a service, it's subject to UAC. If it's an installer, it's probably elevated, and if it's an ordinary app, it probably isn't.
Kate Gregory
UAC is off so that shouldn't be an issue
Matt
So you see the same behaviour when you run as administrator?
Kate Gregory
Yes Administrator account, UAC off, even right click app and Run As Administrator.
Matt
This is something that should generally be run via an elevated installer anyway.
Greg D
+3  A: 

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
Oh, and I haven't done VB in years. Thanks for the mental exercise. :)
David Crowell
I just tried and I am still getting the same result not sure why...
Matt
Actually I got it thanks!!
Matt