我想通过程序为 exe 文件创建桌面快捷方式。我不想使用安装程序来执行此操作。程序中的一段代码可以做到这一点吗?如何?
问问题
24315 次
2 回答
10
添加对 Windows 脚本宿主对象模型的引用
Imports IWshRuntimeLibrary
Private Sub CreateShortCut(ByVal FileName As String, ByVal Title As String)
Try
Dim WshShell As New WshShell
' short cut files have a .lnk extension
Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName, IWshRuntimeLibrary.IWshShortcut)
' set the shortcut properties
With shortCut
.TargetPath = Application.ExecutablePath
.WindowStyle = 1I
.Description = Title
.WorkingDirectory = Application.StartupPath
' the next line gets the first Icon from the executing program
.IconLocation = Application.ExecutablePath & ", 0"
.Arguments = String.Empty
.Save() ' save the shortcut file
End With
Catch ex As System.Exception
MessageBox.Show("Could not create the shortcut" & Environment.NewLine & ex.Message, g_strAppTitleVersion, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
(来源)
于 2011-04-06T16:42:20.960 回答
4
这段代码效果很好
Private Function CreateShortCut(ByVal TargetName As String, ByVal ShortCutPath As String, ByVal ShortCutName As String) As Boolean
Dim oShell As Object
Dim oLink As Object
'you don’t need to import anything in the project reference to create the Shell Object
Try
oShell = CreateObject("WScript.Shell")
oLink = oShell.CreateShortcut(ShortCutPath & "\" & ShortCutName & ".lnk")
oLink.TargetPath = TargetName
oLink.WindowStyle = 1
oLink.Save()
Catch ex As Exception
End Try
End Function
于 2016-11-15T21:21:18.667 回答