2

我有这个代码,它给了麻烦:

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,快捷方式都不会出现。我已经在 SO 和其他各种网站上查找了很多类似的问题。

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

我错过了什么?

4

2 回答 2

3

您的代码中有两个错误:

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
于 2015-01-03T09:54:17.417 回答
2
 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
于 2018-09-23T04:57:44.490 回答