Objects that are inside the library will use the WshShell class and the class WshShortcut. The first of these is the actual shell of Windows Scripting Host and, among the many methods available, the method has CreateShortcut () as an argument that provides the full name of the shortcut path and extension, and returns an object of WshShortcut WshUrlShortcut class or class (actually the method, since COM, returns an object but we can cast the result to the class or WshShortcut WshUrlShortcut at will). Calling CreateShortcut not physically create the link but the link is created using the object returned by the method. For more info on the class WshShell refer to the following link
WshShortcut therefore represents a link (the WshUrlShortcut, in fact, a subset of properties WshShortcut and is designed for web link) and provides all the properties that we need to define the same link (eg icon, working directory, description, etc.. etc...) For more info on the class WshShortcut refer to the following link .
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
(
WshShell ByVal
shortcutFullName
- 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
()
Catch ex As
Exception Exception
- 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 )
Throw Description = Me
. - End Try
- 3) If the instance of the shortcut was created, are valued properties:
- shortcut. TargetPath = Me . TargetPath
- shortcut. WindowStyle = CInt
- ( Me . WindowStyle)
shortcut. WorkingDirectory =
Me
. WorkingDirectory
- 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)
Exception Throw End
- 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
(shortcutFullname)
. WshNormalNoFocus - 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
. WshMinimizedFocus
MinimizedNoFocus =
- 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
0 comments:
Post a Comment