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

时间:2012-02-26 16:31:55

标签: vb.net path target 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

这是否有解决方法?

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

CreateShortcut()无法按预期使用属性中具有灰色目标的某些快捷方式,如Adobe Reader和Microsoft Word。目标路径最终是c:\ windows \ installer(icon?)下的东西。