Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet information about a shortcut in Visual Basic 2005
DescriptionThis example shows how to get information about a shortcut in Visual Basic 2005.
Keywordsshortcut, link, desktop shortcut, Visual Basic 2005
CategoriesWindows, Files and Directories
 
Not that you must add a reference to the COM library "Microsoft Shell Controls and Automation."

The GetShortcutInfo function returns information about a shortcut. It creates a Shell32.Shell object and uses its NameSpace method to get a Folder object representing the folder that contains the shortcut. It then gets a FolderItem object representing the shortcut.

If the file is really a shortcut, then the FolderItem object's properties give information about the shortcut.

 
' Get information about this link.
' Return an error message if there's a problem.
Private Function GetShortcutInfo(ByVal full_name As String, _
    ByRef name As String, ByRef path As String, ByVal descr _
    As String, ByRef working_dir As String, ByRef args As _
    String) As String
    Try
        ' Make a Shell object.
        Dim shl As New Shell32.Shell()

        ' Get the shortcut's folder and name.
        Dim shortcut_path As String = _
            full_name.Substring(0, _
            full_name.LastIndexOf("\"))
        Dim shortcut_name As String = _
            full_name.Substring(full_name.LastIndexOf("\") _
            + 1)
        If Not shortcut_name.EndsWith(".lnk") Then _
            shortcut_name &= ".lnk"

        ' Get the shortcut's folder.
        Dim shortcut_folder As Shell32.Folder = _
            shl.NameSpace(shortcut_path)

        ' Get the shortcut's file.
        Dim folder_item As Shell32.FolderItem = _
            shortcut_folder.Items.Item(shortcut_name)
        If folder_item Is Nothing Then
            Return "Cannot find shortcut file '" & _
                full_name & "'"
        ElseIf Not folder_item.IsLink Then
            ' It's not a link.
            Return "File '" & full_name & "' isn't a " & _
                "shortcut."
        Else
            ' Display the shortcut's information.
            Dim lnk As Shell32.ShellLinkObject = _
                folder_item.GetLink
            name = folder_item.Name
            descr = lnk.Description
            path = lnk.Path
            working_dir = lnk.WorkingDirectory
            args = lnk.Arguments
            Return ""
        End If

    Catch ex As Exception
        Return ex.Message
    End Try
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated