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

时间:2015-01-03 03:55:21

标签: vb.net shortcut lnk startup-folder

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

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和其他各种网站上查找了很多类似的问题。

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

我错过了什么?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

 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