2

I found this code:

    Public Shared Function GetLnkTarget(ByVal 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

It works for some .lnk files, but for example if I add my Skype.exe desktop link it produces:

C:\Windows\Installer\{...}\SkypeIcon.exe

Is there a fix for this?

user670186
  • 2,588
  • 6
  • 37
  • 55
  • Hmm, looks a lot like you are actually using the GetIconLocation() method. – Hans Passant Feb 26 '12 at 18:14
  • What does that mean regarding my problem in this case? Not sure how it explains why it works for some Links but not for others (for Example it does not work for the Skype link on my desktop) – user670186 Feb 27 '12 at 23:41

2 Answers2

1

Try this:

Function GetTargetPath(ByVal FileName As String)

    Dim Obj As Object
    Obj = CreateObject("WScript.Shell")

    Dim Shortcut As Object
    Shortcut = Obj.CreateShortcut(FileName)
    GetTargetPath = Shortcut.TargetPath


End Function

    Private Sub Teste_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

MsgBox(GetTargetPath("C:\ProgramData\Microsoft\Windows\Start Menu\BitTorrent.lnk"))

'here you chose the location of .lnk file

End Sub
Hyrokumata
  • 11
  • 1
0

CreateShortcut() doesn't work as expected with certain shortcuts that have a greyed out target in the properties, like Adobe Reader and Microsoft Word. The targetpath ends up being something under c:\windows\installer (icon?).

js2010
  • 23,033
  • 6
  • 64
  • 66