2

I made a explorer.exe clone, with a treeview and a listview etc.

Now I need to handle clicking a .lnk file, so I need the destination path of a .lnk file..

Dee
  • 67
  • 1
  • 4
  • Hi and welcome to the community! Please provide some code. People get pretty vicious around here pretty quickly if you don't. What have you tried already? – Mattsjo Aug 15 '13 at 08:54
  • possible duplicate of [.NET read binary contents of .lnk file](http://stackoverflow.com/questions/2565885/net-read-binary-contents-of-lnk-file) –  Aug 15 '13 at 08:55
  • 3
    Once again, NOT off topic and not a bad question – John Cruz Jul 19 '16 at 14:01

2 Answers2

6

You can use a WshShell Object:

You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.

and use its CreateShortcut method:

Creates a new shortcut, or opens an existing shortcut.

The resulting WshShortcut object contains a TargetPath property, which is what you're looking for.

Example:

Dim shell = CreateObject("WScript.Shell")
Dim path  = shell.CreateShortcut(path_to_your_link).TargetPath
sloth
  • 99,095
  • 21
  • 171
  • 219
2

I'd try;

Public Shared Function GetLnkTarget(lnkPath As String) As String
    Dim shl = New Shell32.Shell()
    ' Move this to class scope
    lnkPath = System.IO.Path.GetFullPath(lnkPath)
    Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath))
    Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath))
    Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject)
    Return lnk.Target.Path
End Function

You need to reference a COM object; Microsoft Shell Controls And Automation.

Dennis
  • 1,528
  • 2
  • 16
  • 31