.
The class that we want to bring down a number of properties for the shortcut of the definition and a method for rescuing the same (in a similar way as it does the WshShortcut) and encapsulate the use of the two previous classes of scripting.
Imports
IWshRuntimeLibrary PublicClass
Shortcut
     Public
SubNew
- As
- String )
-          Me
- .ShortcutFullName = shortcutFullName      End Sub
-
-      Private _ShortcutFullName As String      Public Property ShortcutFullName As
- String
-          Get              Return
- _ShortcutFullName
-          End Get          Set
- ( ByVal value As String )              If
- String .IsNullOrWhiteSpace(value) Then
- Throw New ArgumentNullException
- ( "ShortcutFullName" , "ShortcutFullName can not be empty!"
- ) _ShortcutFullName = value & # 160; End September
- End Property      Public Property TargetPath As String
-      Public
- Property WindowStyle As ShortcutWindowsStyle
- = ShortcutWindowsStyle .NormalFocus      Public
- Property
- Hotkey As String      Public Property
- IconPath As String      Public Property IconIndex As
- Int16 = 0      Public Property Description As
- String      Public Property WorkingDirectory As
- String      Public Property Arguments As
- String      Public Function
- Save() As Boolean          Dim retval = False
-          Dim shortCut As IWshShortcut = Nothing
-          Dim shell As WshShell = Nothing
-          Try             shell = New
- WshShell ()          Catch ex As Exception
-              Throw          End Try
-          If
- shell IsNot Nothing Then
-              Try                 shortCut = CType
- ( shell .CreateShortcut( Me
- .ShortcutFullName), IWshShortcut )              Catch
- ex As Exception                  Throw
-              End Try
-              If shortCut IsNot Nothing Then
-                  shortCut .TargetPath = Me .TargetPath
-                  shortCut .WindowStyle = CInt
- ( Me .WindowStyle)                  shortCut
- .Description = Me .Description                  ShortCut . working directory = Me . WorkingDirectory
- & # 160;.. shortcut IconLocation = String format ( "{0}, {1}"
- , Me . IconPath, Me . IconIndex) & # 160; shortCut
- .Arguments = Me .Arguments                  If
- Not String .IsNullOrWhiteSpace( Me
- .Hotkey) Then                      shortCut .Hotkey = Me .Hotkey                  End
- If
-                  Try                      shortCut .Save()                     retval = System.IO. File .Exists( Me
- .ShortcutFullName)                  Catch ex As
- Exception                      Throw
-                  End Try
-              End
- If          End
- If          Return retVal
-      End
- Function End Class
- As we can see, the method Save () (which, compared to the amount of WshShortcut returns a result) perform the following steps: 1) Create a instance of the WshShell:
- Dim shell As WshShell = Nothing
- Try
()
- Throw End Try
- 2) If the instance of the shell has been created creates an instance of the class WshShortcut:
- Try shortcut = CType
- ( shell. CreateShortcut ( Me
- . ShortcutFullName) IWshShortcut )
- End Try
- 3) If the instance of the shortcut was created, are valued properties:
- shortcut. TargetPath = Me . TargetPath
- shortcut. WindowStyle = CInt
- ( Me . WindowStyle)
- shortcut. IconLocation String = . Format ( "{0}, {1}"
- , Me . IconPath, Me . IconIndex)
- shortcut. Arguments = Me . Arguments If
- Not String . IsNullOrWhiteSpace ( Me
- . Hotkey) Then shortcut. Hotkey = Me . Hotkey End If
- 4) method is called Save () WshShortcut class and checked the lnk file is actually created: Try
- shortcut. Save () retval = System.IO. File
- . Exists ( Me . ShortcutFullName)
- Try
- Un possibile utilizzo della classe è il seguente:
- Dim desktop = Environment .GetFolderPath( Environment
- . SpecialFolder .DesktopDirectory)
- Dim shortcutFullname = System.IO. Path .Combine(desktop, "Doc.lnk"
shortcut =
New- shortcut .WindowStyle = ShortcutWindowsStyle .NormalNoFocus shortcut .Description = "I miei documenti"
- shortcut. TargetPath = Environment . GetFolderPath ( Environment
- . SpecialFolder . MyDocuments) shortcut. Save ()
- This example creates a link called Doc lnk user's desktop that allows you to open documents.
- Finally we note that one of the properties of the class we created is the Shortcut enumeration:
- Public Enum ShortcutWindowsStyle NormalFocus =
- WshWindowStyle . WshNormalFocus NormalNoFocus =
WshWindowStyle
. WshMaximizedFocusMinimizeFocus =
WshWindowStyle
- WshWindowStyle
- . WshMinimizedNoFocus Hide = WshWindowStyle . WshHide End Enum
- that defines the possible values \u200b\u200bof the style of the shortcut window encapsulating the similar enumeration of WshWindowStyle IWshRuntimeLibrary.
- Technorati tags: IWshRuntimeLibrary ,
- WshShell , WskShortcut ,
- WshUrlShortcut , Shortcut ,
- Link , . lnk, VB.NET