Search code examples
vb.netfilelnk

How can I find the destination of a .lnk file in vb.net


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..


Solution

  • 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.