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



我发现了这个代码:

    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:WindowsInstaller{...}SkypeIcon.exe

有解决办法吗?

试试这个:

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:ProgramDataMicrosoftWindowsStart MenuBitTorrent.lnk"))
'here you chose the location of .lnk file
End Sub

CreateShortcut()对于某些属性中目标为灰色的快捷方式(如Adobe Reader和Microsoft Word)无法正常工作。目标路径最终是c:\windows\anstaller(图标?)下的内容。

相关内容

  • 没有找到相关文章

最新更新