如何获取互联网快捷方式的URL(.url)?

时间:2017-12-05 12:40:20

标签: vb.net url desktop-shortcut

我的客户端在他的桌面上有一些互联网快捷方式(*.url),我想通过VB应用程序获取他们的URL并将它们用作变量。

知道我该怎么做?

1 个答案:

答案 0 :(得分:3)

*.lnk*.appref-ms个文件sample on MSDN
但似乎也适用于*.url - 文件。

来自网站的引用:

  

要检查文件是否是快捷方式并解析快捷方式路径,请执行以下操作:   使用COM库 Microsoft Shell控件和自动化。这个   库被添加到Visual Studio项目的引用中。

代码:

Public Function IsShortcut(strPath As String) As Boolean 
    If Not File.Exists(strPath) Then 
        Return False 
    End If 

    Dim directory As String = Path.GetDirectoryName(strPath) 
    Dim strFile As String = Path.GetFileName(strPath) 

    Dim shell As Shell32.Shell = New Shell32.Shell() 
    Dim folder As Shell32.Folder = shell.NameSpace(directory) 
    Dim folderItem As Shell32.FolderItem = folder.ParseName(strFile) 

    If folderItem IsNot Nothing Then 
        Return folderItem.IsLink 
    End If 

    Return False 
End Function

Public Function ResolveShortcut(strPath As String) As String 
    If IsShortcut(strPath) Then 
        Dim directory As String = Path.GetDirectoryName(strPath) 
        Dim strFile As String = Path.GetFileName(strPath) 

        Dim shell As Shell32.Shell = New Shell32.Shell() 
        Dim folder As Shell32.Folder = shell.NameSpace(directory) 
        Dim folderItem As Shell32.FolderItem = folder.ParseName(strFile) 

        Dim link As Shell32.ShellLinkObject = folderItem.GetLink 

        Return link.Path 
    End If 

    Return String.Empty 
End Function