Verknüpfung erstellen

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von FireEmerald.

    Verknüpfung erstellen

    Hallo Zusammen,

    es gibt hier im Forum einige, meist ältere Beiträge, zu dem Thema wie erstellt man Verknüpfungen / Links.

    Was mich jedoch etwas stört ist wie diese Codes aufgebaut sind. Das Beispiel nutzt als Ansatz Microsoft Shell Controls And Automation (Imports Shell32).

    Beispielcode:

    VB.NET-Quellcode

    1. Public Function CreateShortcut(ByVal _LinkFullPath As String, _
    2. ByVal _LinkFullTargetPath As String, _
    3. Optional ByVal _Description As String = "", _
    4. Optional ByVal _Arguments As String = "", _
    5. Optional ByVal _WorkingDir As String = "") As Boolean
    6. Try
    7. Dim _Shell As New Shell
    8. Dim _Folder As Folder
    9. Dim _LinkObject As ShellLinkObject
    10. ' Ordner und Dateinamen extrahieren
    11. Dim _LinkFolderPath As String = _LinkFullPath.Substring(0, _LinkFullPath.LastIndexOf("\"))
    12. Dim _LinkFilename As String = _LinkFullPath.Substring(_LinkFullPath.LastIndexOf("\") + 1)
    13. ' Wichtig! Link-Datei erstellen (0 Bytes)
    14. Dim F As Short = FreeFile()
    15. FileOpen(F, _LinkFullPath, OpenMode.Output)
    16. FileClose(F)
    17. _Folder = _Shell.NameSpace(_LinkFolderPath)
    18. oLink = oFolder.Items.Item(sFile).GetLink
    19. ' Eigenschaften der Verknüpfung
    20. With _LinkObject
    21. If _Arguments.Length > 0 Then .Arguments = _Arguments
    22. If _Description.Length > 0 Then .Description = _Description
    23. If _WorkingDir.Length > 0 Then .WorkingDirectory = _WorkingDir
    24. .Path = _LinkFullTargetPath
    25. ' Verknüpfung speichern
    26. .Save()
    27. End With
    28. ' Objekte zerstören
    29. _LinkObject = Nothing
    30. _Folder = Nothing
    31. _Shell = Nothing
    32. Return True
    33. Catch ex As Exception
    34. ' Fehler! ggf. Link-Datei löschen, falls bereit erstellt
    35. If System.IO.File.Exists(_LinkFullPath) Then Kill(_LinkFullPath)
    36. Return False
    37. End Try
    38. End Function
    Source: vbarchiv.net/tipps/details.php?id=1601

    Gibt's denn keine Möglichkeit mit den Boardmitteln von .NET einen Link zu erstellen?
    Wie habt Ihr in euren Anwendungen etwas derartiges realisiert?

    Gruß, FireEmerald
    Es gab schonmal mehr Antworten... seis drum.
    Hab mich etwas umgeschaut und für folgende Variante entschieden:

    Anwendungsbeispiel:

    VB.NET-Quellcode

    1. Option Strict On
    2. Option Explicit On
    3. Imports System.Environment
    4. Private Sub Main()
    5. Dim _DesktopDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    6. CreateShortcut(_DesktopDirectory & "\app.lnk", "C:\folder\app.exe", "Application in C", "Arg1 Arg2", "app.exe, 1", "CTRL+SHIFT+F", WindowStyle.Normal)
    7. End Sub


    VB.NET-Quellcode

    1. '// [ALT+F7] / Einstellungen -> Verweise -> Hinzufügen -> COM -> Windows Script Host Object Model
    2. '// [ALT+F7] / Settings -> References -> Add -> COM -> Windows Script Host Object Model
    3. Option Strict On
    4. Option Explicit On
    5. Imports IWshRuntimeLibrary
    6. ''' <summary>
    7. ''' Assigns a window style to a shortcut, or identifies the type of window style used by a shortcut.
    8. ''' </summary>
    9. ''' <remarks>
    10. ''' WindowStyle Property: http://msdn.microsoft.com/en-us/library/w88k7fw2(v=vs.84).aspx
    11. ''' </remarks>
    12. Private Enum WindowStyle As Integer
    13. Normal = 1
    14. Maximized = 3
    15. Minimized = 7
    16. End Enum
    17. ''' <summary>
    18. ''' Creates a new shortcut with the given arguments. If the shortcut already exist, it will be updated with the new arguments.
    19. ''' </summary>
    20. ''' <param name="_ShortcutPath">The destination path where the shortcut will be created, including the filename with extension (.lnk).</param>
    21. ''' <param name="_TargetPath">The target path of the file where the shortcut will point, including the filename with extension.</param>
    22. ''' <param name="_Description">Description of the shortcut. Will be shown on mouse over or in the shortcut settings.</param>
    23. ''' <param name="_Arguments">The Arguments property contains the WshArguments object (a collection of arguments).</param>
    24. ''' <param name="_IconLocation">Consists of the file name and an index associated with the icon. (app.exe, 0)</param>
    25. ''' <remarks>
    26. ''' Source: http://msdn.microsoft.com/en-us/library/fywyxt64(VS.85,ide).aspx
    27. ''' WshShortcut Object: http://msdn.microsoft.com/en-us/library/xk6kst2k(v=vs.84).aspx
    28. ''' </remarks>
    29. Private Sub CreateShortcut(ByVal _ShortcutPath As String, _
    30. ByVal _TargetPath As String, _
    31. Optional _Description As String = "", _
    32. Optional _Arguments As String = "", _
    33. Optional _IconLocation As String = ",0", _
    34. Optional _Hotkey As String = "", _
    35. Optional _WindowStyle As WindowStyle = WindowStyle.Normal)
    36. Dim _Shell As New WshShell
    37. Dim _Shortcut As IWshShortcut = DirectCast(_Shell.CreateShortcut(_ShortcutPath), IWshShortcut)
    38. With _Shortcut
    39. .Arguments = _Arguments
    40. .Description = _Description
    41. .Hotkey = _Hotkey
    42. .IconLocation = _IconLocation
    43. .TargetPath = _TargetPath
    44. .WindowStyle = _WindowStyle
    45. '.WorkingDirectory = "C:\folder"
    46. .Save()
    47. End With
    48. End Sub


    Gruß, FireEmerald