VB.NET/C#代码访问链接(LNK)的目标路径的文件会产生一些错误的路径

问题描述:

我发现这个代码:VB.NET/C#代码访问链接(LNK)的目标路径的文件会产生一些错误的路径

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 

它适用于一些.LNK文件,但例如,如果我加我它产生的Skype.exe桌面链接:

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

是否有修复?

+0

嗯,看起来很像你实际上使用GetIconLocation()方法。 – 2012-02-26 18:14:56

+0

在这种情况下对我的问题意味着什么?不知道它是如何解释为什么它适用于某些链接,但不适用于其他链接(例如,它不适用于我的桌面上的Skype链接) – user670186 2012-02-27 23:41:15

试试这个:

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 

CreateShortcut()作为具有一定的快捷方式出现一个灰色的目标的属性,如Adobe Reader和Microsoft Word中不正常工作。目标路径最终成为c:\ windows \ installer(图标?)下的东西。