2015-01-03 89 views
1

创建快捷方式我有这样的代码和它给的烦恼:在启动文件夹(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多少,快捷方式都不显示。我已经在S.O和其他各种网站上看到很多类似的问题。

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

我错过了什么?

+0

什么Windows版本? – OneFineDay

+0

@OneFineDay Windows 7家庭普通版 – soulblazer

回答

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