【问题标题】:VB.NET / C# code to access target path of link (lnk) files produces some wrong pathsVB.NET / C# 代码访问链接(lnk)文件的目标路径会产生一些错误的路径
【发布时间】:2012-02-26 16:31:55
【问题描述】:

我找到了这段代码:

    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

有解决办法吗?

【问题讨论】:

  • 嗯,看起来很像您实际上在使用 GetIconLocation() 方法。
  • 在这种情况下我的问题意味着什么?不确定它如何解释为什么它适用于某些链接但不适用于其他链接(例如,它不适用于我桌面上的 Skype 链接)

标签: vb.net path target lnk


【解决方案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

【讨论】:

    【解决方案2】:

    CreateShortcut() 对于某些在属性中具有灰色目标的快捷方式(如 Adob​​e Reader 和 Microsoft Word)无法正常工作。目标路径最终位于 c:\windows\installer(图标?)下。

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      相关资源
      最近更新 更多