在启动文件夹中创建快捷方式 (VB.NET)

Create Shortcut in StartUp folder (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

据我了解,这就是您在 Startup 中创建快捷方式的方法。但是,无论我如何调用这个 Sub,快捷方式都不会出现。我已经在 S.O 和其他各种网站上查找了很多类似的问题。

我什至尝试从其他应用程序创建快捷方式,但仍然没有按预期显示。

我错过了什么?

您的代码中有两个错误:

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) 你拼错了 Description 所以更改:

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