Создать ярлык в папке автозагрузки (VB.NET)

У меня есть этот код, и он доставляет неприятности:

Imports IWshRuntimeLibrary
Imports Shell32

Public Sub CreateShortcutInStartUp(ByVal Descrip As String)
    Dim WshShell As WshShell = New WshShell()
    Dim ShortcutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
    Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(ShortcutPath &    
    Application.ProductName & ".lnk"), IWshShortcut)
    Shortcut.TargetPath = Application.ExecutablePath
    Shortcut.WorkingDirectory = Application.StartupPath
    Shortcut.Descripcion = Descrip
    Shortcut.Save()
End Sub

Согласно тому, что я прочитал, вы создаете ярлык в автозагрузке. Но, как бы я ни называл этот Sub, ярлык не отображается. Я УЖЕ смотрю на множество подобных вопросов вокруг SO и различных других сайтов.

Я даже пытался создать ярлык из другого приложения и все еще не отображается, как ожидалось.

Что мне не хватает?

2 ответа

Решение

У вас есть две ошибки в вашем коде:

1) Путь не соединяется должным образом, поэтому измените это:

Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(ShortcutPath & Application.ProductName & ".lnk"), IWshShortcut)

к этому:

Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(System.IO.Path.Combine(ShortcutPath, Application.ProductName) & ".lnk"), IWshShortcut)

2) Вы написали неправильное описание, поэтому измените:

Shortcut.Descripcion = Descrip

к этому:

Shortcut.Description = Descrip

Вот фиксированная подпрограмма:

Public Sub CreateShortcutInStartUp(ByVal Descrip As String)
    Dim WshShell As WshShell = New WshShell()
    Dim ShortcutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
    Dim Shortcut As IWshShortcut = CType(WshShell.CreateShortcut(System.IO.Path.Combine(ShortcutPath, Application.ProductName) & ".lnk"), IWshShortcut)
    Shortcut.TargetPath = Application.ExecutablePath
    Shortcut.WorkingDirectory = Application.StartupPath
    Shortcut.Description = Descrip
    Shortcut.Save()
End Sub
 Imports Microsoft.Win32 and
 Imports IWshRuntimeLibrary

СОЗДАТЬ ШОРТКУ

Private Sub btnCreateShortcut_Click_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateShortcut.Click
    'To create Start shortcut in the windows Startup folder: 
    Dim WshShell As WshShell = New WshShell
    Dim SRT As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
    'Fixing the Shortcut Location instead of StartupScreenLocker
    'Name your Shortcut, Example \ScreenLocker.Ink
    Dim ShortcutPath As String = SRT & "\ScreenLocker.lnk"

    'Add shortcut.
    Dim Shortcut As IWshRuntimeLibrary.IWshShortcut
    Shortcut = CType(WshShell.CreateShortcut(ShortcutPath), IWshRuntimeLibrary.IWshShortcut)
    Shortcut.TargetPath = Application.ExecutablePath
    Shortcut.WorkingDirectory = Application.StartupPath
    Shortcut.Save()
End Sub

УДАЛИТЬ ШОРТКУ

Private Sub btnDeleteShortcut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteShortcut.Click

    'To create Start shortcut in the windows Startup folder: 
    Dim Shortcut As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)

     'Name the Shortcut you want to delete, Example \ScreenLocker.Ink
    Dim ShortcutPath As String = Shortcut & "\ScreenLocker.lnk"

    'To delete the shortcut:
    If IO.File.Exists(ShortcutPath) Then
        IO.File.Delete(ShortcutPath)
    End If
End Sub
Другие вопросы по тегам