如何在没有安装程序的情况下在vb.net中在桌面上创建快捷方式

发布于 2024-10-31 02:52:54 字数 68 浏览 4 评论 0原文

我想通过程序创建exe文件的桌面快捷方式。 我不想使用安装程序来执行此操作。 程序中的一段代码可以做到这一点吗?如何实现?

I want to create desktop shortcut for exe file through program.
I do not want to use installer to do this.
Can a piece of code in program do this?How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2024-11-07 02:52:54

Chasler 几年前就回答了这个问题此处

添加对 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

(来源)

Chasler has answered that question a couple of years ago here on SO.

Add a reference to the Windows Script Host Object Model

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

(Source)

回忆躺在深渊里 2024-11-07 02:52:54

此代码运行良好

 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

致谢

This code works very well

 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

Credits

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文