【问题标题】:how to create shortcut on desktop in vb.net without installer如何在没有安装程序的情况下在 vb.net 桌面上创建快捷方式
【发布时间】:2011-04-06 16:29:58
【问题描述】:

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

【问题讨论】:

    标签: vb.net winforms windows-installer desktop-shortcut


    【解决方案1】:

    Chasler 几年前就回答了这个问题here on SO

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

    (Source)

    【讨论】:

      【解决方案2】:

      这段代码运行良好

       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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 1970-01-01
        • 2013-07-26
        相关资源
        最近更新 更多