Thursday, December 30, 2010

Jenna Jameson Pierced Tits

Create a shortcut with VB.NET

  • Inspired by a post appeared on the MSDN forum I would like to offer you a tip on how to create a shortcut using VB.NET. To create a shortcut we can proceed in two ways: either we study the structure of the file. Lnk and write a class that is able to recreate that structure or use Windows Scripting Host.
  • The first solution is feasible, but cumbersome because the structure of a lnk file is not trivial. Anyone interested to see how it is composed, internally, a lnk file can download the following reference guide ( link).
  • I would like to offer you the second street and will create a class that encapsulates the use of Windows Scripting Host. The object model of Windows Scripting Host is contained in IWshRuntimeLibrary dll that can be referenced in our project, using the COM tab of the window to add the reference:
  • 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

      image

    Public

    Class

    Shortcut

     

         Public

    Sub

    New
    (
    ByVal
    shortcutFullName
      As
    1. String )
    2.         
    3. Me
    4. .ShortcutFullName = shortcutFullName      End Sub
    5.  
    6.      Private _ShortcutFullName As String      Public Property ShortcutFullName
    7. As
    8. String
    9.          Get             
    10. Return
    11. _ShortcutFullName
    12.          End Get         
    13. Set
    14. ( ByVal value As String )             
    15. If
    16. String .IsNullOrWhiteSpace(value) Then
    17. Throw New ArgumentNullException
    18. ( "ShortcutFullName" ,
    19. "ShortcutFullName can not be empty!"
    20. ) _ShortcutFullName = value & # 160; End September
    21. End Property        Public Property TargetPath As String
    22.     
    23. Public
    24. Property WindowStyle As
    25. ShortcutWindowsStyle
    26. = ShortcutWindowsStyle .NormalFocus     
    27. Public
    28. Property
    29. Hotkey As String      Public
    30. Property
    31. IconPath As String      Public Property IconIndex
    32. As
    33. Int16 = 0      Public Property Description
    34. As
    35. String      Public Property WorkingDirectory
    36. As
    37. String      Public Property Arguments
    38. As
    39. String        Public
    40. Function
    41. Save() As Boolean          Dim retval =
    42. False
    43.          Dim shortCut As IWshShortcut =
    44. Nothing
    45.          Dim shell As WshShell = Nothing
    46.          Try             shell =
    47. New
    48. WshShell ()          Catch ex As
    49. Exception
    50.              Throw          End
    51. Try
    52.         
    53. If
    54. shell IsNot Nothing
    55. Then
    56.              Try                 shortCut =
    57. CType
    58. ( shell .CreateShortcut(
    59. Me
    60. .ShortcutFullName), IWshShortcut )             
    61. Catch
    62. ex As Exception                  Throw
    63.              End
    64. Try
    65.              If shortCut IsNot Nothing
    66. Then
    67.                  shortCut .TargetPath = Me .TargetPath
    68.                  shortCut .WindowStyle =
    69. CInt
    70. ( Me .WindowStyle)                 
    71. shortCut
    72. .Description = Me .Description                  ShortCut . working directory = Me . WorkingDirectory
    73. & # 160;.. shortcut IconLocation = String format (
    74. "{0}, {1}"
    75. , Me . IconPath, Me . IconIndex) & # 160;
    76. shortCut
    77. .Arguments = Me .Arguments                 
    78. If
    79. Not String .IsNullOrWhiteSpace(
    80. Me
    81. .Hotkey) Then                      shortCut .Hotkey = Me .Hotkey                 
    82. End
    83. If  
    84.                  Try                      shortCut .Save()                     retval = System.IO. File .Exists(
    85. Me
    86. .ShortcutFullName)                  Catch ex
    87. As
    88. Exception                     
    89. Throw
    90.                  End
    91. Try
    92.             
    93. End
    94. If         
    95. End
    96. If          Return retVal
    97.      End
    98. Function End Class
    99. 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:
    100. Dim shell As WshShell = Nothing
    101. Try
    shell = New
    WshShell

    ()

    Catch ex As
    Exception
    1. Throw End Try
    2. 2) If the instance of the shell has been created creates an instance of the class WshShortcut:
    3. Try shortcut = CType
    4. ( shell. CreateShortcut (
    5. Me
    6. . ShortcutFullName) IWshShortcut )
    Catch ex As
    Exception

    Throw
    1. End Try
    2. 3) If the instance of the shortcut was created, are valued properties:
    3. shortcut. TargetPath = Me . TargetPath
    4. shortcut. WindowStyle = CInt
    5. ( Me . WindowStyle)
    shortcut.
    Description = Me
    .
    Description

    shortcut. WorkingDirectory =
    Me
    . WorkingDirectory
    1. shortcut. IconLocation String = . Format (
    2. "{0}, {1}"
    3. , Me . IconPath, Me . IconIndex)
    4. shortcut. Arguments = Me . Arguments
    5. If
    6. Not String . IsNullOrWhiteSpace (
    7. Me
    8. . Hotkey) Then shortcut. Hotkey = Me . Hotkey
    9. End If
    10. 4) method is called Save () WshShortcut class and checked the lnk file is actually created: Try
    11. shortcut. Save () retval = System.IO. File
    12. . Exists ( Me . ShortcutFullName)
    Catch ex As

    Exception Throw End
    1. Try
    2. Un possibile utilizzo della classe รจ il seguente:
    3. Dim desktop = Environment .GetFolderPath(
    4. Environment
    5. . SpecialFolder .DesktopDirectory)
    6. Dim shortcutFullname = System.IO. Path .Combine(desktop,
    7. "Doc.lnk"
    )
    Dim

    shortcut =

    New
    Shortcut
    (shortcutFullname)
    1. shortcut .WindowStyle = ShortcutWindowsStyle .NormalNoFocus shortcut .Description =
    2. "I miei documenti"
    3. shortcut. TargetPath = Environment . GetFolderPath (
    4. Environment
    5. . SpecialFolder . MyDocuments) shortcut. Save ()
    6. This example creates a link called Doc lnk user's desktop that allows you to open documents.
    7. Finally we note that one of the properties of the class we created is the Shortcut enumeration:
    8. Public Enum ShortcutWindowsStyle
    9. NormalFocus =
    10. WshWindowStyle . WshNormalFocus
    11. NormalNoFocus =
    WshWindowStyle
    . WshNormalNoFocus
    MaximizedFocus =

    WshWindowStyle

    . WshMaximizedFocus

    MinimizeFocus =

    WshWindowStyle

    . WshMinimizedFocus
    MinimizedNoFocus =
      WshWindowStyle
    1. . WshMinimizedNoFocus Hide = WshWindowStyle . WshHide
    2. End Enum
    3. that defines the possible values \u200b\u200bof the style of the shortcut window encapsulating the similar enumeration of WshWindowStyle IWshRuntimeLibrary.
    4. Technorati tags: IWshRuntimeLibrary
    5. ,
    6. WshShell , WskShortcut
    7. ,
    8. WshUrlShortcut , Shortcut
    9. ,
    10. Link , . lnk, VB.NET

    0 comments:

    Post a Comment