Advertisement
PiToLoKo

ShortcutManager for VB.NET - By Elektro

Feb 26th, 2014
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 35.44 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 02-16-2014
  4. ' ***********************************************************************
  5. ' <copyright file="ShortcutManager.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Usage Examples "
  11.  
  12. #Region " Example 1 "
  13.  
  14. 'Private Sub Test()
  15.  
  16. '    ' Tries to resolve a shortcut which has changed their Target location.
  17. '    ShortcutManager.ResolveUi("C:\Truncated Shortcut.lnk", New IntPtr(1))
  18. '    ShortcutManager.ResolveNoUi("C:\Truncated Shortcut.lnk")
  19.  
  20. '    ' Creates a new Shortcut file
  21. '    ShortcutManager.CreateShortcut("C:\Shortcut.lnk",
  22. '                                   "C:\TargetFile.ext",
  23. '                                   "C:\",
  24. '                                   "Description",
  25. '                                   "-Arguments",
  26. '                                   "C:\Icon.ico", 0,
  27. '                                   ShortcutManager.HotkeyModifiers.ALT Or ShortcutManager.HotkeyModifiers.CONTROL,
  28. '                                   Keys.F1,
  29. '                                   ShortcutManager.ShortcutWindowState.Normal)
  30.  
  31. '    ' Gets Shortcut file information
  32. '    Dim ShortcutInfo As ShortcutManager.ShortcutInfo =
  33. '        ShortcutManager.GetInfo("C:\Shortcut.lnk")
  34.  
  35. '    Dim sb As New System.Text.StringBuilder
  36.  
  37. '    With ShortcutInfo
  38.  
  39. '        sb.AppendLine(String.Format(" ""{0}"" ", .ShortcutFile))
  40. '        sb.AppendLine(String.Format("------------------------"))
  41. '        sb.AppendLine(String.Format("Description: {0}", .Description))
  42. '        sb.AppendLine(String.Format("Target: {0}", .Target))
  43. '        sb.AppendLine(String.Format("Arguments: {0}", .Arguments))
  44. '        sb.AppendLine(String.Format("Target Is Directory?: {0}", CStr(.IsDirectory)))
  45. '        sb.AppendLine(String.Format("Target Is File?: {0}", CStr(.IsFile)))
  46. '        sb.AppendLine(String.Format("WorkingDir: {0}", .WorkingDir))
  47. '        sb.AppendLine(String.Format("DirectoryName: {0}", .DirectoryName))
  48. '        sb.AppendLine(String.Format("FileName: {0}", .FileName))
  49. '        sb.AppendLine(String.Format("FileExtension: {0}", .FileExtension))
  50. '        sb.AppendLine(String.Format("DriveLetter: {0}", .DriveLetter))
  51. '        sb.AppendLine(String.Format("Icon: {0}", .Icon))
  52. '        sb.AppendLine(String.Format("Icon Index: {0}", CStr(.IconIndex)))
  53. '        sb.AppendLine(String.Format("Hotkey (Hex): {0}", CStr(.Hotkey)))
  54. '        sb.AppendLine(String.Format("Hotkey (Str): {0} + {1}", .Hotkey_Modifier.ToString, .Hotkey_Key.ToString))
  55. '        sb.AppendLine(String.Format("Window State: {0}", .WindowState.ToString))
  56.  
  57. '    End With
  58.  
  59. '    MessageBox.Show(sb.ToString)
  60.  
  61. '    ' Modify attributes of an existing lnk file
  62. '    For Each lnk As String In IO.Directory.GetFiles("C:\", "*.lnk")
  63.  
  64. '        Dim info As ShortcutManager.ShortcutInfo = ShortcutManager.GetInfo(lnk)
  65.  
  66. '        ShortcutManager.ModifyShortcut(lnk, info.Target.Replace("Something old", "Something new"))
  67.  
  68. '    Next lnk
  69.  
  70. 'End Sub
  71.  
  72. #End Region
  73.  
  74. #Region " Example 2 "
  75.  
  76. 'Public Class Form1
  77.  
  78. '    ''' <summary>
  79. '    ''' The directory of the shortcut to create.
  80. '    ''' </summary>
  81. '    ReadOnly ShortcutDir As String =
  82. '        IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Links")
  83.  
  84. '    ''' <summary>
  85. '    ''' The filename of the shortcut to create.
  86. '    ''' </summary>
  87. '    ReadOnly ShortcutName As String =
  88. '        "MyProgram.lnk"
  89.  
  90. '    ReadOnly FileInfo As IO.FileInfo =
  91. '        New IO.FileInfo(IO.Path.Combine(My.Application.Info.DirectoryPath(),
  92. '                                        Process.GetCurrentProcess().MainModule.ModuleName))
  93.  
  94. '    Private Shadows Sub Load() Handles MyBase.Load
  95.  
  96. '        Dim Shortcut As New ShortcutManager.ShortcutInfo
  97.  
  98. '        With Shortcut
  99.  
  100. '            .ShortcutFile = IO.Path.Combine(ShortcutDir, ShortcutName)
  101. '            .Target = FileInfo.FullName
  102. '            .Arguments = Nothing
  103. '            .WorkingDir = FileInfo.DirectoryName
  104. '            .Description = "MyProgram"
  105. '            .Icon = FileInfo.FullName
  106. '            .IconIndex = 0
  107. '            .WindowState = ShortcutManager.ShortcutWindowState.Normal
  108.  
  109. '        End With
  110.  
  111. '        ShortcutManager.CreateShortcut(Shortcut)
  112.  
  113. '    End Sub
  114.  
  115. 'End Class
  116.  
  117. #End Region
  118.  
  119. #End Region
  120.  
  121. #Region " Imports "
  122.  
  123. Imports System.Runtime.InteropServices
  124. Imports System.Text
  125. Imports System.IO
  126.  
  127. #End Region
  128.  
  129. #Region " ShortcutManager "
  130.  
  131. ''' <summary>
  132. ''' Performs Shortcut related operations.
  133. ''' </summary>
  134. Public Class ShortcutManager
  135.  
  136. #Region " Variables "
  137.  
  138.     Private Shared lnk As New ShellLink()
  139.     Private Shared lnk_data As New WIN32_FIND_DATAW()
  140.  
  141.     Private Shared lnk_arguments As New StringBuilder(260)
  142.     Private Shared lnk_description As New StringBuilder(260)
  143.     Private Shared lnk_target As New StringBuilder(260)
  144.     Private Shared lnk_workingdir As New StringBuilder(260)
  145.     Private Shared lnk_iconpath As New StringBuilder(260)
  146.     Private Shared lnk_iconindex As Integer = -1
  147.     Private Shared lnk_hotkey As Short = -1
  148.     Private Shared lnk_windowstate As ShortcutWindowState = ShortcutWindowState.Normal
  149.  
  150. #End Region
  151.  
  152. #Region " P/Invoke "
  153.  
  154.     <DllImport("shfolder.dll", CharSet:=CharSet.Auto)>
  155.     Private Shared Function SHGetFolderPath(
  156.            ByVal hwndOwner As IntPtr,
  157.            ByVal nFolder As Integer,
  158.            ByVal hToken As IntPtr,
  159.            ByVal dwFlags As Integer,
  160.            ByVal lpszPath As StringBuilder
  161.     ) As Integer
  162.     End Function
  163.  
  164.     <Flags()>
  165.     Private Enum SLGP_FLAGS
  166.  
  167.         ''' <summary>
  168.         ''' Retrieves the standard short (8.3 format) file name.
  169.         ''' </summary>
  170.         SLGP_SHORTPATH = &H1
  171.  
  172.         ''' <summary>
  173.         ''' Retrieves the Universal Naming Convention (UNC) path name of the file.
  174.         ''' </summary>
  175.         SLGP_UNCPRIORITY = &H2
  176.  
  177.         ''' <summary>
  178.         ''' Retrieves the raw path name.
  179.         ''' A raw path is something that might not exist and may include environment variables that need to be expanded.
  180.         ''' </summary>
  181.         SLGP_RAWPATH = &H4
  182.  
  183.     End Enum
  184.  
  185.     <Flags()>
  186.     Private Enum SLR_FLAGS
  187.  
  188.         ''' <summary>
  189.         ''' Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
  190.         ''' the high-order word of fFlags can be set to a time-out value that specifies the
  191.         ''' maximum amount of time to be spent resolving the link. The function returns if the
  192.         ''' link cannot be resolved within the time-out duration. If the high-order word is set
  193.         ''' to zero, the time-out duration will be set to the default value of 3,000 milliseconds
  194.         ''' (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
  195.         ''' duration, in milliseconds.
  196.         ''' </summary>
  197.         SLR_NO_UI = &H1
  198.  
  199.         ''' <summary>
  200.         ''' If the link object has changed, update its path and list of identifiers.
  201.         ''' If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine,
  202.         ''' whether or not the link object has changed.
  203.         ''' </summary>
  204.         SLR_UPDATE = &H4
  205.  
  206.         ''' <summary>
  207.         ''' Do not update the link information
  208.         ''' </summary>
  209.         SLR_NOUPDATE = &H8
  210.  
  211.         ''' <summary>
  212.         ''' Do not execute the search heuristics
  213.         ''' </summary>
  214.         SLR_NOSEARCH = &H10
  215.  
  216.         ''' <summary>
  217.         ''' Do not use distributed link tracking
  218.         ''' </summary>
  219.         SLR_NOTRACK = &H20
  220.  
  221.         ''' <summary>
  222.         ''' Disable distributed link tracking.
  223.         ''' By default, distributed link tracking tracks removable media,
  224.         ''' across multiple devices based on the volume name.
  225.         ''' It also uses the Universal Naming Convention (UNC) path to track remote file systems,
  226.         ''' whose drive letter has changed.
  227.         ''' Setting SLR_NOLINKINFO disables both types of tracking.
  228.         ''' </summary>
  229.         SLR_NOLINKINFO = &H40
  230.  
  231.         ''' <summary>
  232.         ''' Call the Microsoft Windows Installer
  233.         ''' </summary>
  234.         SLR_INVOKE_MSI = &H80
  235.  
  236.     End Enum
  237.  
  238.     ''' <summary>
  239.     ''' Stores information about a shortcut file.
  240.     ''' </summary>
  241.     Public Class ShortcutInfo
  242.  
  243.         ''' <summary>
  244.         ''' Shortcut file full path.
  245.         ''' </summary>
  246.         Public Property ShortcutFile As String
  247.  
  248.         ''' <summary>
  249.         ''' Shortcut Comment/Description.
  250.         ''' </summary>
  251.         Public Property Description As String
  252.  
  253.         ''' <summary>
  254.         ''' Shortcut Target Arguments.
  255.         ''' </summary>
  256.         Public Property Arguments As String
  257.  
  258.         ''' <summary>
  259.         ''' Shortcut Target.
  260.         ''' </summary>
  261.         Public Property Target As String
  262.  
  263.         ''' <summary>
  264.         ''' Shortcut Working Directory.
  265.         ''' </summary>
  266.         Public Property WorkingDir As String
  267.  
  268.         ''' <summary>
  269.         ''' Shortcut Icon Location.
  270.         ''' </summary>
  271.         Public Property Icon As String
  272.  
  273.         ''' <summary>
  274.         ''' Shortcut Icon Index.
  275.         ''' </summary>
  276.         Public Property IconIndex As Integer
  277.  
  278.         ''' <summary>
  279.         ''' Shortcut Hotkey combination.
  280.         ''' Is represented as Hexadecimal.
  281.         ''' </summary>
  282.         Public Property Hotkey As Short
  283.  
  284.         ''' <summary>
  285.         ''' Shortcut Hotkey modifiers.
  286.         ''' </summary>
  287.         Public Property Hotkey_Modifier As HotkeyModifiers
  288.  
  289.         ''' <summary>
  290.         ''' Shortcut Hotkey Combination.
  291.         ''' </summary>
  292.         Public Property Hotkey_Key As Keys
  293.  
  294.         ''' <summary>
  295.         ''' Shortcut Window State.
  296.         ''' </summary>
  297.         Public Property WindowState As ShortcutWindowState
  298.  
  299.         ''' <summary>
  300.         ''' Indicates if the target is a file.
  301.         ''' </summary>
  302.         Public Property IsFile As Boolean
  303.  
  304.         ''' <summary>
  305.         ''' Indicates if the target is a directory.
  306.         ''' </summary>
  307.         Public Property IsDirectory As Boolean
  308.  
  309.         ''' <summary>
  310.         ''' Shortcut target drive letter.
  311.         ''' </summary>
  312.         Public Property DriveLetter As String
  313.  
  314.         ''' <summary>
  315.         ''' Shortcut target directory name.
  316.         ''' </summary>
  317.         Public Property DirectoryName As String
  318.  
  319.         ''' <summary>
  320.         ''' Shortcut target filename.
  321.         ''' (File extension is not included in name)
  322.         ''' </summary>
  323.         Public Property FileName As String
  324.  
  325.         ''' <summary>
  326.         ''' Shortcut target file extension.
  327.         ''' </summary>
  328.         Public Property FileExtension As String
  329.  
  330.     End Class
  331.  
  332.     ''' <summary>
  333.     ''' Hotkey modifiers for a shortcut file.
  334.     ''' </summary>
  335.     <Flags()>
  336.     Public Enum HotkeyModifiers As Short
  337.  
  338.         ''' <summary>
  339.         ''' The SHIFT key.
  340.         ''' </summary>
  341.         SHIFT = 1
  342.  
  343.         ''' <summary>
  344.         ''' The CTRL key.
  345.         ''' </summary>
  346.         CONTROL = 2
  347.  
  348.         ''' <summary>
  349.         ''' The ALT key.
  350.         ''' </summary>
  351.         ALT = 4
  352.  
  353.         ''' <summary>
  354.         ''' None.
  355.         ''' Specifies any hotkey modificator.
  356.         ''' </summary>
  357.         NONE = 0
  358.  
  359.     End Enum
  360.  
  361.     ''' <summary>
  362.     ''' The Window States for a shortcut file.
  363.     ''' </summary>
  364.     Public Enum ShortcutWindowState As Integer
  365.  
  366.         ''' <summary>
  367.         ''' Shortcut Window is at normal state.
  368.         ''' </summary>
  369.         Normal = 1
  370.  
  371.         ''' <summary>
  372.         ''' Shortcut Window is Maximized.
  373.         ''' </summary>
  374.         Maximized = 3
  375.  
  376.         ''' <summary>
  377.         ''' Shortcut Window is Minimized.
  378.         ''' </summary>
  379.         Minimized = 7
  380.  
  381.     End Enum
  382.  
  383.     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
  384.     Private Structure WIN32_FIND_DATAW
  385.         Public dwFileAttributes As UInteger
  386.         Public ftCreationTime As Long
  387.         Public ftLastAccessTime As Long
  388.         Public ftLastWriteTime As Long
  389.         Public nFileSizeHigh As UInteger
  390.         Public nFileSizeLow As UInteger
  391.         Public dwReserved0 As UInteger
  392.         Public dwReserved1 As UInteger
  393.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
  394.         Public cFileName As String
  395.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)>
  396.         Public cAlternateFileName As String
  397.     End Structure
  398.  
  399.     ''' <summary>
  400.     ''' The IShellLink interface allows Shell links to be created, modified, and resolved
  401.     ''' </summary>
  402.     <ComImport(),
  403.     InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
  404.     Guid("000214F9-0000-0000-C000-000000000046")>
  405.     Private Interface IShellLinkW
  406.  
  407.         ''' <summary>
  408.         ''' Retrieves the path and file name of a Shell link object.
  409.         ''' </summary>
  410.         Sub GetPath(<Out(), MarshalAs(UnmanagedType.LPWStr)>
  411.                     ByVal pszFile As StringBuilder,
  412.                     ByVal cchMaxPath As Integer,
  413.                     ByRef pfd As WIN32_FIND_DATAW,
  414.                     ByVal fFlags As SLGP_FLAGS)
  415.  
  416.         ''' <summary>
  417.         ''' Retrieves the list of item identifiers for a Shell link object.
  418.         ''' </summary>
  419.         Sub GetIDList(ByRef ppidl As IntPtr)
  420.  
  421.         ''' <summary>
  422.         ''' Sets the pointer to an item identifier list (PIDL) for a Shell link object.
  423.         ''' </summary>
  424.         Sub SetIDList(ByVal pidl As IntPtr)
  425.  
  426.         ''' <summary>
  427.         ''' Retrieves the description string for a Shell link object.
  428.         ''' </summary>
  429.         Sub GetDescription(<Out(), MarshalAs(UnmanagedType.LPWStr)>
  430.                            ByVal pszName As StringBuilder,
  431.                            ByVal cchMaxName As Integer)
  432.  
  433.         ''' <summary>
  434.         ''' Sets the description for a Shell link object.
  435.         ''' The description can be any application-defined string.
  436.         ''' </summary>
  437.         Sub SetDescription(<MarshalAs(UnmanagedType.LPWStr)>
  438.                            ByVal pszName As String)
  439.  
  440.         ''' <summary>
  441.         ''' Retrieves the name of the working directory for a Shell link object.
  442.         ''' </summary>
  443.         Sub GetWorkingDirectory(<Out(), MarshalAs(UnmanagedType.LPWStr)>
  444.                                 ByVal pszDir As StringBuilder,
  445.                                 ByVal cchMaxPath As Integer)
  446.  
  447.         ''' <summary>
  448.         ''' Sets the name of the working directory for a Shell link object.
  449.         ''' </summary>
  450.         Sub SetWorkingDirectory(<MarshalAs(UnmanagedType.LPWStr)>
  451.                                 ByVal pszDir As String)
  452.  
  453.         ''' <summary>
  454.         ''' Retrieves the command-line arguments associated with a Shell link object.
  455.         ''' </summary>
  456.         Sub GetArguments(<Out(), MarshalAs(UnmanagedType.LPWStr)>
  457.                          ByVal pszArgs As StringBuilder,
  458.                          ByVal cchMaxPath As Integer)
  459.  
  460.         ''' <summary>
  461.         ''' Sets the command-line arguments for a Shell link object.
  462.         ''' </summary>
  463.         Sub SetArguments(<MarshalAs(UnmanagedType.LPWStr)>
  464.                          ByVal pszArgs As String)
  465.  
  466.         ''' <summary>
  467.         ''' Retrieves the hot key for a Shell link object.
  468.         ''' </summary>
  469.         Sub GetHotkey(ByRef pwHotkey As Short)
  470.  
  471.         ''' <summary>
  472.         ''' Sets a hot key for a Shell link object.
  473.         ''' </summary>
  474.         Sub SetHotkey(ByVal wHotkey As Short)
  475.  
  476.         ''' <summary>
  477.         ''' Retrieves the show command for a Shell link object.
  478.         ''' </summary>
  479.         Sub GetShowCmd(ByRef piShowCmd As Integer)
  480.  
  481.         ''' <summary>
  482.         ''' Sets the show command for a Shell link object.
  483.         ''' The show command sets the initial show state of the window.
  484.         ''' </summary>
  485.         Sub SetShowCmd(ByVal iShowCmd As ShortcutWindowState)
  486.  
  487.         ''' <summary>
  488.         ''' Retrieves the location (path and index) of the icon for a Shell link object.
  489.         ''' </summary>
  490.         Sub GetIconLocation(<Out(), MarshalAs(UnmanagedType.LPWStr)>
  491.                             ByVal pszIconPath As StringBuilder,
  492.                             ByVal cchIconPath As Integer,
  493.                             ByRef piIcon As Integer)
  494.  
  495.         ''' <summary>
  496.         ''' Sets the location (path and index) of the icon for a Shell link object.
  497.         ''' </summary>
  498.         Sub SetIconLocation(<MarshalAs(UnmanagedType.LPWStr)>
  499.                             ByVal pszIconPath As String,
  500.                             ByVal iIcon As Integer)
  501.  
  502.         ''' <summary>
  503.         ''' Sets the relative path to the Shell link object.
  504.         ''' </summary>
  505.         Sub SetRelativePath(<MarshalAs(UnmanagedType.LPWStr)>
  506.                             ByVal pszPathRel As String,
  507.                             ByVal dwReserved As Integer)
  508.  
  509.         ''' <summary>
  510.         ''' Attempts to find the target of a Shell link,
  511.         ''' even if it has been moved or renamed.
  512.         ''' </summary>
  513.         Sub Resolve(ByVal hwnd As IntPtr,
  514.                     ByVal fFlags As SLR_FLAGS)
  515.  
  516.         ''' <summary>
  517.         ''' Sets the path and file name of a Shell link object
  518.         ''' </summary>
  519.         Sub SetPath(ByVal pszFile As String)
  520.  
  521.     End Interface
  522.  
  523.     <ComImport(), Guid("0000010c-0000-0000-c000-000000000046"),
  524.     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
  525.     Private Interface IPersist
  526.  
  527.         <PreserveSig()>
  528.         Sub GetClassID(ByRef pClassID As Guid)
  529.  
  530.     End Interface
  531.  
  532.     <ComImport(), Guid("0000010b-0000-0000-C000-000000000046"),
  533.     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
  534.     Private Interface IPersistFile
  535.         Inherits IPersist
  536.  
  537.         Shadows Sub GetClassID(ByRef pClassID As Guid)
  538.  
  539.         <PreserveSig()>
  540.         Function IsDirty() As Integer
  541.  
  542.         <PreserveSig()>
  543.         Sub Load(<[In](), MarshalAs(UnmanagedType.LPWStr)>
  544.                  pszFileName As String,
  545.                  dwMode As UInteger)
  546.  
  547.         <PreserveSig()>
  548.         Sub Save(<[In](), MarshalAs(UnmanagedType.LPWStr)>
  549.                  pszFileName As String,
  550.                  <[In](), MarshalAs(UnmanagedType.Bool)>
  551.                  fRemember As Boolean)
  552.  
  553.         <PreserveSig()>
  554.         Sub SaveCompleted(<[In](), MarshalAs(UnmanagedType.LPWStr)>
  555.                           pszFileName As String)
  556.  
  557.         <PreserveSig()>
  558.         Sub GetCurFile(<[In](), MarshalAs(UnmanagedType.LPWStr)>
  559.                        ppszFileName As String)
  560.  
  561.     End Interface
  562.  
  563.     ' "CLSID_ShellLink" from "ShlGuid.h"
  564.     <ComImport(),
  565.     Guid("00021401-0000-0000-C000-000000000046")>
  566.     Private Class ShellLink
  567.     End Class
  568.  
  569. #End Region
  570.  
  571. #Region " Public Methods "
  572.  
  573.     ''' <summary>
  574.     ''' Resolves the target of a shortcut.
  575.     ''' If shortcut can't be resolved, an error message would be displayed.
  576.     ''' This is usefull when the target path of a shortcut file is changed from a driveletter for example,
  577.     ''' then the shortcut file need to be resolved before trying to retrieve the target path.
  578.     ''' </summary>
  579.     ''' <param name="ShortcutFile">
  580.     ''' The shortcut file to resolve.
  581.     ''' </param>
  582.     ''' <param name="hwnd">
  583.     ''' The new handle pointer that would be generated
  584.     ''' for the window which should display the error message (if any).
  585.     ''' </param>
  586.     Public Shared Sub ResolveUi(ShortcutFile As String, hwnd As IntPtr)
  587.         LoadShortcut(ShortcutFile)
  588.         DirectCast(lnk, IShellLinkW).Resolve(hwnd, SLR_FLAGS.SLR_UPDATE)
  589.     End Sub
  590.  
  591.     ''' <summary>
  592.     ''' Resolves the target of a shortcut.
  593.     ''' If shortcut can't be resolved, any error message would be displayed.
  594.     ''' This is usefull when the target path of a shortcut file is changed from a driveletter for example,
  595.     ''' then the shortcut file need to be resolved before trying to retrieve the target path.
  596.     ''' </summary>
  597.     ''' <param name="ShortcutFile">
  598.     ''' The shortcut file to resolve.
  599.     ''' </param>
  600.     Public Shared Sub ResolveNoUi(ByVal ShortcutFile As String)
  601.         LoadShortcut(ShortcutFile)
  602.         DirectCast(lnk, IShellLinkW).Resolve(IntPtr.Zero, SLR_FLAGS.SLR_UPDATE Or SLR_FLAGS.SLR_NO_UI)
  603.     End Sub
  604.  
  605.     ''' <summary>
  606.     ''' Returns the description of a shortcut file.
  607.     ''' </summary>
  608.     ''' <param name="ShortcutFile">
  609.     ''' The shortcut file to retrieve the info.
  610.     ''' </param>
  611.     Public Shared Function GetDescription(ByVal ShortcutFile As String) As String
  612.         LoadShortcut(ShortcutFile)
  613.         lnk_description.Clear()
  614.         DirectCast(lnk, IShellLinkW).GetDescription(lnk_description, lnk_description.Capacity)
  615.         Return lnk_description.ToString()
  616.     End Function
  617.  
  618.     ''' <summary>
  619.     ''' Returns the Arguments of a shortcut file.
  620.     ''' </summary>
  621.     ''' <param name="ShortcutFile">
  622.     ''' The shortcut file to retrieve the info.
  623.     ''' </param>
  624.     Public Shared Function GetArguments(ByVal ShortcutFile As String) As String
  625.         LoadShortcut(ShortcutFile)
  626.         lnk_arguments.Clear()
  627.         DirectCast(lnk, IShellLinkW).GetArguments(lnk_arguments, lnk_arguments.Capacity)
  628.         Return lnk_arguments.ToString()
  629.     End Function
  630.  
  631.     ''' <summary>
  632.     ''' Returns the path and filename of a shortcut file.
  633.     ''' </summary>
  634.     ''' <param name="ShortcutFile">
  635.     ''' The shortcut file to retrieve the info.
  636.     ''' </param>
  637.     Public Shared Function GetFullPath(ByVal ShortcutFile As String) As String
  638.         LoadShortcut(ShortcutFile)
  639.         lnk_target.Clear()
  640.         DirectCast(lnk, IShellLinkW).GetPath(lnk_target, lnk_target.Capacity, lnk_data, SLGP_FLAGS.SLGP_UNCPRIORITY)
  641.         Return lnk_target.ToString()
  642.     End Function
  643.  
  644.     ''' <summary>
  645.     ''' Returns the working directory of a shortcut file.
  646.     ''' </summary>
  647.     ''' <param name="ShortcutFile">
  648.     ''' The shortcut file to retrieve the info.
  649.     ''' </param>
  650.     Public Shared Function GetWorkingDir(ByVal ShortcutFile As String) As String
  651.         LoadShortcut(ShortcutFile)
  652.         lnk_workingdir.Clear()
  653.         DirectCast(lnk, IShellLinkW).GetWorkingDirectory(lnk_workingdir, lnk_workingdir.Capacity)
  654.         Return lnk_workingdir.ToString()
  655.     End Function
  656.  
  657.     ''' <summary>
  658.     ''' Returns the Hotkey of a shortcut file.
  659.     ''' </summary>
  660.     ''' <param name="ShortcutFile">
  661.     ''' The shortcut file to retrieve the info.
  662.     ''' </param>
  663.     Public Shared Function GetHotkey(ByVal ShortcutFile As String) As Short
  664.         LoadShortcut(ShortcutFile)
  665.         lnk_hotkey = -1
  666.         DirectCast(lnk, IShellLinkW).GetHotkey(lnk_hotkey)
  667.         Return lnk_hotkey
  668.     End Function
  669.  
  670.     ''' <summary>
  671.     ''' Returns the Window State of a shortcut file.
  672.     ''' </summary>
  673.     ''' <param name="ShortcutFile">
  674.     ''' The shortcut file to retrieve the info.
  675.     ''' </param>
  676.     Public Shared Function GetWindowStyle(ByVal ShortcutFile As String) As ShortcutWindowState
  677.         LoadShortcut(ShortcutFile)
  678.         DirectCast(lnk, IShellLinkW).GetShowCmd(lnk_windowstate)
  679.         Return lnk_windowstate
  680.     End Function
  681.  
  682.     ''' <summary>
  683.     ''' Returns the Icon location of a shortcut file.
  684.     ''' </summary>
  685.     ''' <param name="ShortcutFile">
  686.     ''' The shortcut file to retrieve the info.
  687.     ''' </param>
  688.     ''' <param name="IconIndex">
  689.     ''' Optional Integer type variable to store the IconIndex.
  690.     ''' </param>
  691.     Public Shared Function GetIconLocation(ByVal ShortcutFile As String,
  692.                                             Optional ByRef IconIndex As Integer = 0) As String
  693.         LoadShortcut(ShortcutFile)
  694.         lnk_iconpath.Clear()
  695.         DirectCast(lnk, IShellLinkW).GetIconLocation(lnk_iconpath, lnk_iconpath.Capacity, IconIndex)
  696.         Return lnk_iconpath.ToString()
  697.     End Function
  698.  
  699.     ''' <summary>
  700.     ''' Retrieves all the information about a shortcut file.
  701.     ''' </summary>
  702.     ''' <param name="ShortcutFile">
  703.     ''' The shortcut file to retrieve the info.
  704.     ''' </param>
  705.     Public Shared Function GetInfo(ByVal ShortcutFile As String) As ShortcutInfo
  706.  
  707.         ' Load Shortcut
  708.         LoadShortcut(ShortcutFile)
  709.  
  710.         ' Clean objects
  711.         Clean()
  712.  
  713.         ' Retrieve Shortcut Info
  714.         DirectCast(lnk, IShellLinkW).GetDescription(lnk_description, lnk_description.Capacity)
  715.         DirectCast(lnk, IShellLinkW).GetArguments(lnk_arguments, lnk_arguments.Capacity)
  716.         DirectCast(lnk, IShellLinkW).GetPath(lnk_target, lnk_target.Capacity, lnk_data, SLGP_FLAGS.SLGP_UNCPRIORITY)
  717.         DirectCast(lnk, IShellLinkW).GetWorkingDirectory(lnk_workingdir, lnk_workingdir.Capacity)
  718.         DirectCast(lnk, IShellLinkW).GetIconLocation(lnk_iconpath, lnk_iconpath.Capacity, lnk_iconindex)
  719.         DirectCast(lnk, IShellLinkW).GetHotkey(lnk_hotkey)
  720.         DirectCast(lnk, IShellLinkW).GetShowCmd(lnk_windowstate)
  721.  
  722.         ' Return Shortcut Info
  723.         Return New ShortcutInfo With {
  724.             .ShortcutFile = ShortcutFile,
  725.             .Description = lnk_description.ToString,
  726.             .Arguments = lnk_arguments.ToString,
  727.             .Target = lnk_target.ToString,
  728.             .Icon = lnk_iconpath.ToString,
  729.             .IconIndex = lnk_iconindex,
  730.             .WorkingDir = lnk_workingdir.ToString,
  731.             .Hotkey = Hex(lnk_hotkey),
  732.             .Hotkey_Modifier = [Enum].Parse(GetType(HotkeyModifiers), GetHiByte(lnk_hotkey)),
  733.             .Hotkey_Key = [Enum].Parse(GetType(Keys), GetLoByte(lnk_hotkey)),
  734.             .WindowState = lnk_windowstate,
  735.             .IsFile = File.Exists(lnk_target.ToString),
  736.             .IsDirectory = Directory.Exists(lnk_target.ToString),
  737.             .DriveLetter = lnk_target.ToString.Substring(0, 1),
  738.             .DirectoryName = lnk_target.ToString.Substring(0, lnk_target.ToString.LastIndexOf("\")),
  739.             .FileName = lnk_target.ToString.Split("\").LastOrDefault.Split(".").FirstOrDefault,
  740.             .FileExtension = lnk_target.ToString.Split(".").LastOrDefault
  741.         }
  742.  
  743.     End Function
  744.  
  745.     ''' <summary>
  746.     ''' Creates a shortcut file.
  747.     ''' </summary>
  748.     ''' <param name="FilePath">
  749.     ''' The filepath to create the shortcut.
  750.     ''' </param>
  751.     ''' <param name="Target">
  752.     ''' The target file or directory.
  753.     ''' </param>
  754.     ''' <param name="WorkingDirectory">
  755.     ''' The working directory os the shortcut.
  756.     ''' </param>
  757.     ''' <param name="Description">
  758.     ''' The shortcut description.
  759.     ''' </param>
  760.     ''' <param name="Arguments">
  761.     ''' The target file arguments.
  762.     ''' This value only should be set when target is an executable file.
  763.     ''' </param>
  764.     ''' <param name="Icon">
  765.     ''' The icon location of the shortcut.
  766.     ''' </param>
  767.     ''' <param name="IconIndex">
  768.     ''' The icon index of the icon file.
  769.     ''' </param>
  770.     ''' <param name="HotKey_Modifier">
  771.     ''' The hotkey modifier(s) which should be used for the hotkey combination.
  772.     ''' <paramref name="HotkeyModifiers"/> can be one or more modifiers.
  773.     ''' </param>
  774.     ''' <param name="HotKey_Key">
  775.     ''' The key used in combination with the <paramref name="HotkeyModifiers"/> for hotkey combination.
  776.     ''' </param>
  777.     ''' <param name="WindowState">
  778.     ''' The Window state for the target.
  779.     ''' </param>
  780.     Public Shared Sub CreateShortcut(ByVal FilePath As String,
  781.                                      ByVal Target As String,
  782.                                      Optional ByVal WorkingDirectory As String = Nothing,
  783.                                      Optional ByVal Description As String = Nothing,
  784.                                      Optional ByVal Arguments As String = Nothing,
  785.                                      Optional ByVal Icon As String = Nothing,
  786.                                      Optional ByVal IconIndex As Integer = Nothing,
  787.                                      Optional ByVal HotKey_Modifier As HotkeyModifiers = Nothing,
  788.                                      Optional ByVal HotKey_Key As Keys = Nothing,
  789.                                      Optional ByVal WindowState As ShortcutWindowState = ShortcutWindowState.Normal)
  790.  
  791.         ' Load Shortcut
  792.         LoadShortcut(FilePath)
  793.  
  794.         ' Clean objects
  795.         Clean()
  796.  
  797.         ' Set Shortcut Info
  798.         DirectCast(lnk, IShellLinkW).SetPath(Target)
  799.  
  800.         DirectCast(lnk, IShellLinkW).SetWorkingDirectory(If(WorkingDirectory IsNot Nothing,
  801.                                                             WorkingDirectory,
  802.                                                             Path.GetDirectoryName(Target)))
  803.  
  804.         DirectCast(lnk, IShellLinkW).SetDescription(Description)
  805.         DirectCast(lnk, IShellLinkW).SetArguments(Arguments)
  806.         DirectCast(lnk, IShellLinkW).SetIconLocation(Icon, IconIndex)
  807.  
  808.         DirectCast(lnk, IShellLinkW).SetHotkey(If(HotKey_Modifier + HotKey_Key <> 0,
  809.                                                   Convert.ToInt16(CInt(HotKey_Modifier & Hex(HotKey_Key)), 16),
  810.                                                   Nothing))
  811.  
  812.         DirectCast(lnk, IShellLinkW).SetShowCmd(WindowState)
  813.  
  814.         DirectCast(lnk, IPersistFile).Save(FilePath, True)
  815.         DirectCast(lnk, IPersistFile).SaveCompleted(FilePath)
  816.  
  817.     End Sub
  818.  
  819.     ''' <summary>
  820.     ''' Creates a shortcut file.
  821.     ''' </summary>
  822.     ''' <param name="Shortcut">Indicates a ShortcutInfo object.</param>
  823.     Public Shared Sub CreateShortcut(ByVal Shortcut As ShortcutInfo)
  824.  
  825.         CreateShortcut(Shortcut.ShortcutFile,
  826.                        Shortcut.Target,
  827.                        Shortcut.WorkingDir,
  828.                        Shortcut.Description,
  829.                        Shortcut.Arguments,
  830.                        Shortcut.Icon,
  831.                        Shortcut.IconIndex,
  832.                        Shortcut.Hotkey_Modifier,
  833.                        Shortcut.Hotkey_Key,
  834.                        Shortcut.WindowState)
  835.  
  836.     End Sub
  837.  
  838.     ''' <summary>
  839.     ''' Modifies the atributes of an existing shortcut file.
  840.     ''' </summary>
  841.     ''' <param name="ShortcutFile">
  842.     ''' The existing .lnk file to modify.
  843.     ''' </param>
  844.     ''' <param name="Target">
  845.     ''' The target file or directory.
  846.     ''' </param>
  847.     ''' <param name="WorkingDirectory">
  848.     ''' The working directory os the shortcut.
  849.     ''' </param>
  850.     ''' <param name="Description">
  851.     ''' The shortcut description.
  852.     ''' </param>
  853.     ''' <param name="Arguments">
  854.     ''' The target file arguments.
  855.     ''' This value only should be set when target is an executable file.
  856.     ''' </param>
  857.     ''' <param name="Icon">
  858.     ''' The icon location of the shortcut.
  859.     ''' </param>
  860.     ''' <param name="IconIndex">
  861.     ''' The icon index of the icon file.
  862.     ''' </param>
  863.     ''' <param name="HotKey_Modifier">
  864.     ''' The hotkey modifier(s) which should be used for the hotkey combination.
  865.     ''' <paramref name="HotkeyModifiers"/> can be one or more modifiers.
  866.     ''' </param>
  867.     ''' <param name="HotKey_Key">
  868.     ''' The key used in combination with the <paramref name="HotkeyModifiers"/> for hotkey combination.
  869.     ''' </param>
  870.     ''' <param name="WindowState">
  871.     ''' The Window state for the target.
  872.     ''' </param>
  873.     Public Shared Sub ModifyShortcut(ByVal ShortcutFile As String,
  874.                                      Optional ByVal Target As String = Nothing,
  875.                                      Optional ByVal WorkingDirectory As String = Nothing,
  876.                                      Optional ByVal Description As String = Nothing,
  877.                                      Optional ByVal Arguments As String = Nothing,
  878.                                      Optional ByVal Icon As String = Nothing,
  879.                                      Optional ByVal IconIndex As Integer = -1,
  880.                                      Optional ByVal HotKey_Modifier As HotkeyModifiers = -1,
  881.                                      Optional ByVal HotKey_Key As Keys = -1,
  882.                                      Optional ByVal WindowState As ShortcutWindowState = -1)
  883.  
  884.         ' Load Shortcut
  885.         LoadShortcut(ShortcutFile)
  886.  
  887.         ' Clean objects
  888.         Clean()
  889.  
  890.         ' Retrieve Shortcut Info
  891.         DirectCast(lnk, IShellLinkW).GetDescription(lnk_description, lnk_description.Capacity)
  892.         DirectCast(lnk, IShellLinkW).GetArguments(lnk_arguments, lnk_arguments.Capacity)
  893.         DirectCast(lnk, IShellLinkW).GetPath(lnk_target, lnk_target.Capacity, lnk_data, SLGP_FLAGS.SLGP_UNCPRIORITY)
  894.         DirectCast(lnk, IShellLinkW).GetWorkingDirectory(lnk_workingdir, lnk_workingdir.Capacity)
  895.         DirectCast(lnk, IShellLinkW).GetIconLocation(lnk_iconpath, lnk_iconpath.Capacity, lnk_iconindex)
  896.         DirectCast(lnk, IShellLinkW).GetHotkey(lnk_hotkey)
  897.         DirectCast(lnk, IShellLinkW).GetShowCmd(lnk_windowstate)
  898.  
  899.         ' Set Shortcut Info
  900.         DirectCast(lnk, IShellLinkW).SetPath(If(Target IsNot Nothing,
  901.                                                 Target,
  902.                                                 lnk_target.ToString))
  903.  
  904.         DirectCast(lnk, IShellLinkW).SetWorkingDirectory(If(WorkingDirectory IsNot Nothing,
  905.                                                             WorkingDirectory,
  906.                                                             lnk_workingdir.ToString))
  907.  
  908.         DirectCast(lnk, IShellLinkW).SetDescription(If(Description IsNot Nothing,
  909.                                                        Description,
  910.                                                        lnk_description.ToString))
  911.  
  912.         DirectCast(lnk, IShellLinkW).SetArguments(If(Arguments IsNot Nothing,
  913.                                                      Arguments,
  914.                                                      lnk_arguments.ToString))
  915.  
  916.         DirectCast(lnk, IShellLinkW).SetIconLocation(If(Icon IsNot Nothing, Icon, lnk_iconpath.ToString),
  917.                                                      If(IconIndex <> -1, IconIndex, lnk_iconindex))
  918.  
  919.         DirectCast(lnk, IShellLinkW).SetHotkey(If(HotKey_Modifier + HotKey_Key > 0,
  920.                                                   Convert.ToInt16(CInt(HotKey_Modifier & Hex(HotKey_Key)), 16),
  921.                                                   lnk_hotkey))
  922.  
  923.         DirectCast(lnk, IShellLinkW).SetShowCmd(If(WindowState <> -1,
  924.                                                    WindowState,
  925.                                                    lnk_windowstate))
  926.  
  927.         DirectCast(lnk, IPersistFile).Save(ShortcutFile, True)
  928.         DirectCast(lnk, IPersistFile).SaveCompleted(ShortcutFile)
  929.  
  930.  
  931.     End Sub
  932.  
  933. #End Region
  934.  
  935. #Region " Private Methods "
  936.  
  937.     ''' <summary>
  938.     ''' Loads the shortcut object to retrieve information.
  939.     ''' </summary>
  940.     ''' <param name="ShortcutFile">
  941.     ''' The shortcut file to retrieve the info.
  942.     ''' </param>
  943.     Private Shared Sub LoadShortcut(ByVal ShortcutFile As String)
  944.         DirectCast(lnk, IPersistFile).Load(ShortcutFile, 0)
  945.     End Sub
  946.  
  947.     ''' <summary>
  948.     ''' Clean the shortcut info objects.
  949.     ''' </summary>
  950.     Private Shared Sub Clean()
  951.         lnk_description.Clear()
  952.         lnk_arguments.Clear()
  953.         lnk_target.Clear()
  954.         lnk_workingdir.Clear()
  955.         lnk_iconpath.Clear()
  956.         lnk_hotkey = -1
  957.         lnk_iconindex = -1
  958.     End Sub
  959.  
  960.     ''' <summary>
  961.     ''' Gets the low order byte of a number.
  962.     ''' </summary>
  963.     Private Shared Function GetLoByte(ByVal Intg As Integer) As Integer
  964.         Return Intg And &HFF&
  965.     End Function
  966.  
  967.     ''' <summary>
  968.     ''' Gets the high order byte of a number.
  969.     ''' </summary>
  970.     Private Shared Function GetHiByte(ByVal Intg As Integer) As Integer
  971.         Return (Intg And &HFF00&) / 256
  972.     End Function
  973.  
  974. #End Region
  975.  
  976. End Class
  977.  
  978. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement