Results 1 to 5 of 5

Thread: Get exe from shortcut no api or vbscript

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Get exe from shortcut no api or vbscript

    Hi this is a small bit of code I made to get the exe path and file from a shortcut, it may need tidying up a bit but I try it on a few shortcuts and seems to work
    anyway comments and suggestions welcome.

    vbnet Code:
    1. Imports System.IO
    2. Imports System.Text
    3.  
    4. Public Class Form1
    5.  
    6.     Public Function GetShortCutLink(ByVal Source As String) As String
    7.         Dim br As BinaryReader
    8.         Dim Bytes(2048) As Byte
    9.         Dim sb As New StringBuilder()
    10.  
    11.         If Not File.Exists(Source) Then
    12.             Throw New FileNotFoundException
    13.         End If
    14.  
    15.         'Open shortcut file for reading.
    16.         br = New BinaryReader(File.OpenRead(Source), Encoding.ASCII)
    17.         'Read 2048 bytes into bytes array.
    18.         Bytes = br.ReadBytes(Bytes.Length)
    19.         br.Close()
    20.  
    21.         For Counter As Integer = 100 To Bytes.Length - 1
    22.             'Check for :\ and
    23.             If Bytes(Counter) = 58 And Bytes(Counter + 1) = 92 And Bytes(Counter - 2) <> 47 Then
    24.                 Counter = (Counter - 1)
    25.                 Do Until Bytes(Counter) = 0
    26.                     'Build string.
    27.                     sb.Append(Chr(Bytes(Counter)))
    28.                     'INC Counter
    29.                     Counter += 1
    30.                 Loop
    31.                 Exit For
    32.             End If
    33.         Next Counter
    34.         'Return shortcut link.
    35.         Return sb.ToString()
    36.  
    37.     End Function
    38.  
    39.     Private Sub cmdGetLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetLink.Click
    40.         Try
    41.             'Try and get link.
    42.             Dim sLink As String = GetShortCutLink("C:\out\data\Easy GIF Animator Pro 5.2.lnk")
    43.  
    44.             MessageBox.Show(sLink, "Link",
    45.                 MessageBoxButtons.OK, MessageBoxIcon.Information)
    46.         Catch ex As Exception
    47.             MessageBox.Show(ex.Message, "Error",
    48.                             MessageBoxButtons.OK, MessageBoxIcon.Information)
    49.         End Try
    50.     End Sub
    51. End Class

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get exe from shortcut no api or vbscript

    Nice do you understand the rest of the stuff in a .lnk file then or is that the only bit you know how to parse at the moment? I'm just wondering if you'd be able to use this knowledge to write a .NET method that would create your own shortcut files, as that seems to be a real pain to do from managed code at the moment (there's some native shell APIs that will do it but they're not particularly straight forward).
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Get exe from shortcut no api or vbscript

    No that all I know I only really wanted something to read the exe from the shortcut, To create shortcuts I use the VBScript object. Though I am sure you can make a raw shortcut creator in VB with out API or VBScript, just need to get your hands on the file format.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get exe from shortcut no api or vbscript

    Yeah exactly, but I've never come across anywhere that actually explains the LNK file format
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get exe from shortcut no api or vbscript

    Actually, just found this http://msdn.microsoft.com/en-us/libr...(prot.20).aspx which leads you to this detailed document: http://download.microsoft.com/downlo...S-SHLLINK].pdf
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width