Get target of shortcut

bjay55

Freshman
Joined
Mar 19, 2005
Messages
25
Using visual basic .net, I was wondering how I would be able to get the target file of a shortcut (.lnk file). Thank you for any help.
 
I was just wondering the same thing. Also, is it possible to change the target of a shortcut or create a new shortcut? I hope someone can answer this...
 
Shortcuts are in a binary format and, unlike the Linux ln command, there is no way that I have been able to find for creating, manipulating, or dealing with shortcuts on the command line or in a program. The last time I messed with this stuff, about 2 years ago, I found a command line tool online that would do everything I wanted -- reading, creating, deciphering, etc. Of course, now that I think about it, I was writing in Perl at the time so I'm not sure if there are facilities in .Net that will help you or not. But I do know there are free tools out there. Try an internet search and see what turns up.

The tool I found was called shortcut.exe and it was made by Optimum X. Hopefully that will help. And there may be a .Net way to do it too; I'm just not sure off the top of my head.
 
Found Something That Works

I found something that works for getting the link target and description.
I found the function GetLinkInfo, it is in the sheltarg.dll file.

Private Function GetLinkTarget(ByVal filename As String) As String

Dim ret As Integer
Dim path, description As String
path = Space$(256)
description = Space$(256)

ret = GetLinkInfo(Me.Handle, filename, path, description)

GetLinkTarget = path

End Function

Thanks for all your replies.
 
If you're not averse to a little COM pinvoke you can just declare the IShellLink interface, ShellLink coclass and the associated bits and pieces (Win32FindData struct a few enumerations and various other things). Then wrap the IShellLink in a disposable object and you've something that'll let you deal with shortcuts easily.
 
This seems to be the easiest way so far. It lets you read all the attributes of a shortcut as well as change them or create a new shortcut from scratch.

Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim WshShell As Object, Shortcut As Object
        WshShell = CreateObject("WScript.Shell")
        Shortcut = WshShell.CreateShortcut("c:\\Shortcut Test.lnk") 
        MessageBox.Show(Shortcut.targetpath)

End Sub


This code should run as-is...
 
Last edited:
Back
Top