Shortcut/Verknüpfung erstellen in VB.NET

M

marS268

Hallo,

kann mir einer erklären, wie ich diesen Code richtig bei mir einbaue?
Code:
Public Class Form1
Dim Shell As WshShell
Dim Shortcut As WshShortcut

Private Sub cmdMakeLNKFile_Click()

'initialize the object WshShell
Set objShell = New WshShell

'initialize the object WshShortcut
'the complete name of the .lnk file, include full path plus the .LNK file extension
Set objShortcut = objShell.CreateShortcut (txtLnkName.Text)

'the file to be called by the .lnk file, ej. "c:\windows\calc.exe"
objShortcut.TargetPath = txtTarget.Text

'(optional) := any command line supported by the file indicated in txtTarget.Text
'objShortcut.Arguments = xxxx

'(optional) : = a valid icon file : = To use the same icon of the target file, do not use the next line.
'objShortcut.IconLocation = xxxx

'Save the .lnk
objShortcut.Save

End Sub
End Class

Ich habe mir eine ganz normale WindowsForm gemacht (WindowsApplication1) und in diese zwei Textfelder(txtLnkName, txtTarget), sowie einen Button (cmdMakeLNKFile) eingefügt. Dann hab ich in den Einstellungen meines Projektes unter Verweise einen Com-Verweis(Windows Script Host Object Model) hinzugefügt.

Leider moppert der rum:
Fehler 1 Der Typ "WshShell" ist nicht definiert.
Fehler 2 Der Typ "WshShortcut" ist nicht definiert.
Fehler 3 Der Name "objShell" wurde nicht deklariert.
Fehler 4 Der Typ "WshShell" ist nicht definiert.
Fehler 5 Der Name "objShortcut" wurde nicht deklariert.
Fehler 6 Der Name "objShell" wurde nicht deklariert.
Fehler 7 Der Name "objShortcut" wurde nicht deklariert.
Fehler 8 Der Name "objShortcut" wurde nicht deklariert.

Und nu?

Viele Grüße

marS
 
Danke aber hat sich jetzt erledigt. Brauche es doch nicht. :)
Kann der Thread gelöscht werden?
 
Wenn der Thread nicht gelöscht wird und jemand das wissen will und auf diesen Thread gelangt, dann ärgert er sich, da keine Lösung gefunden worden ist.

Meine Lösung:
Ich habe die Shortcuts einfach nicht erstellt.
Ich brauche sie doch nicht.:)
 
Wenn man die Fehlermeldungen genau gelesen hätte und diese mit dem vorliegendem Code vergleicht fällt direkt auf:
1. Es wurde etwas nichts definiert, wahrscheinlich fehlt die entsprechende imports-Anweisung, oder es wurde etwas falsch geschrieben.
2. Es wurde etwas nicht deklariert, also fehlt die Dim Name as Typ Anweisung.

Damit ist jetzt jedem geholfen, der dieses Beispiel benutzen möchte :)
 
Du musst unter My Projekt den Verweiss "Microsoft Shell Controls And Automations", den du unter COM findest, hinzufügen!
Dann müste es gehen!

MfG. Steven2803
 
Lösung:

1) in Projekteigenschaften [ Verweise ] - [ Verweis hinzufügen]-/COM\ "WSHControllerLibary" hinzufügen.
(c:\Windows\System32\wshcon.dll)
Es erscheint der Verweis:
"Windows Script Host Object Model COM 1.0.0.0 True ....... "

2) Füge im Modul ein: "Imports IWshRuntimeLibrary" hinzu

3) Dann muss dein Code gehen.


Ich danke Dir sehr für deine Frage, denn nur dadurch habe ich meine selbe Aufgabe lösen können.
==============================================================================
Gruß Martin (scma53)


Ps. BeispielCode:
Code:
Imports IWshRuntimeLibrary

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ShortcutDir = My.Application.Info.DirectoryPath
        'Dim ShortcutDir = Shell.SpecialFolders("Desktop")

        Dim Shell As New WshShell

        'Dim Shortcut As New WshShortcut
        Dim Shortcut = Shell.CreateShortcut("MyFirlsLngDelTest.lnk") 

        Shortcut.TargetPath = "c:\windows\calc.exe"
        Shortcut.Arguments = ""
        Shortcut.WorkingDirectory = ShortcutDir
        Shortcut.Description = " Microsofts Desktop Rechner"
        Shortcut.WindowStyle = 1 ' 1= normalfenster, 3=maximiert  7= Minimiert (nur in taskleiste)
        Shortcut.Hotkey = "CTRL+SHIFT+F"
        Shortcut.IconLocation = "notepad.exe, 0"
        Shortcut.IconLocation = "c:\windows\calc.exe , 0"

        'Save the .lnk
        Shortcut.Save()

        ' ------------Daten auslesen / anzeigen --------------------
        MsgBox("Fullname = " & Shortcut.fullname & vbCrLf _
            & "Targetpath = " & Shortcut.TargetPath & vbCrLf _
            & "WorkingDirectory = " & Shortcut.WorkingDirectory & vbCrLf _
            & "Arguments = " & Shortcut.Arguments & vbCrLf _
            & "WindowStyle = " & Shortcut.WindowStyle & vbCrLf _
            & "Hotkey = " & Shortcut.Hotkey & vbCrLf _
            & "IconLocation = " & Shortcut.IconLocation & vbCrLf _
            & "Description = " & Shortcut.Description)

        '----------------Link ausführen------------------
        Microsoft.VisualBasic.Shell(Shortcut.TargetPath)

    End Sub

End Class
 
Zuletzt bearbeitet von einem Moderator:
Hmmm.
Also ich habe es genau so versucht...Bei mir kommen diese 2 Meldungen:

Warnung 1 Der in Imports "IWshRuntimeLibrary" angegebene Namespace oder Typ enthält keine öffentlichen Member oder kann nicht gefunden werden. Stellen Sie sicher, dass der Namespace oder der Typ definiert ist und mindestens einen öffentlichen Member enthält und dass der importierte Elementname keine weiteren Aliase enthält. C:\Users\Alex\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 1 9 WindowsApplication1

Fehler 2 Der Typ "WshShell" ist nicht definiert. C:\Users\Alex\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 9 26 WindowsApplication1
 
Zurück