如何在 vb.net 中找到 .lnk 文件的目的地

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18249263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:40:42  来源:igfitidea点击:

How can I find the destination of a .lnk file in vb.net

vb.netfilelnk

提问by Dee

I made a explorer.exe clone, with a treeview and a listview etc.

我做了一个 explorer.exe 克隆,带有树视图和列表视图等。

Now I need to handle clicking a .lnk file, so I need the destination path of a .lnk file..

现在我需要处理点击一个 .lnk 文件,所以我需要一个 .lnk 文件的目标路径..

采纳答案by Lectere

I'd try;

我会尝试;

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

You need to reference a COM object; Microsoft Shell Controls And Automation.

您需要引用一个 COM 对象;Microsoft Shell 控件和自动化。

回答by sloth

You can use a WshShell Object:

您可以使用WshShell Object

You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.

每当您想在本地运行程序、操作注册表的内容、创建快捷方式或访问系统文件夹时,您都可以创建 WshShell 对象。

and use its CreateShortcutmethod:

并使用它的CreateShortcut方法:

Creates a new shortcut, or opens an existing shortcut.

创建新的快捷方式,或打开现有的快捷方式。

The resulting WshShortcutobject contains a TargetPathproperty, which is what you're looking for.

结果WshShortcut对象包含一个TargetPath属性,这就是您要查找的内容。

Example:

例子:

Dim shell = CreateObject("WScript.Shell")
Dim path  = shell.CreateShortcut(path_to_your_link).TargetPath