1 Replies - 1708 Views - Last Post: 03 April 2019 - 05:20 PM

#1 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Create Shortcut Programmatically With Windows Script Host Object Model

Posted 23 March 2019 - 06:04 PM

A user the other day asked me how to create a shortcut to your startup folder in the startmenu. And so I began writing out how I would do it, and since this is kinda a rare question, I am sure it will make for a good Snipped/Tutorial for future readers whom are looking to do the same thing.

  • Create a reference to Windows Script Host Object Model by going to Your Project Name / Add Reference / Com / Windows Script Host Object Model. / Tick it and add it. Then add the imports statement to the top of your page ::
  • Imports IWshRuntimeLibrary


You will likely see squiggly errors if you don't add the imports line. Dim PdName As String = Application.ProductName will give you the name of your application product. This CreateSCut(PdName, "MyApp") calls the subroutine, and passes the parameters of the product name and your description of your applications shortcut to the method to execute.

Here is a working animation of it in progress ::

Posted Image

Here is the code you need to create your own little shortcuts.
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Environment
Imports IWshRuntimeLibrary

Public Class Form1
    Dim PdName As String = Application.ProductName
    Private Sub PopulateBtn_Click(sender As Object, e As EventArgs) Handles PopulateBtn.Click
        CreateSCut(PdName, "MyApp")
    End Sub
    Public Sub CreateSCut(ByVal pdtName As String, ByVal Descrip As String)
        Dim ShellSrpt As WshShell = New WshShell()
        Dim startupPath As String = GetFolderPath(SpecialFolder.Startup)
        Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, pdtName) & ".lnk"), IWshShortcut)
        myShortcut.TargetPath = Application.ExecutablePath
        myShortcut.WorkingDirectory = Application.StartupPath
        myShortcut.Description = Descrip
        myShortcut.Save()
    End Sub
End Class


Stepping through the code. It's straight forward, but we will do it anyway ::

Dim ShellSrpt As WshShell = New WshShell() - Creates a new instance of this object model shell.

Dim startupPath As String = GetFolderPath(SpecialFolder.Startup) - Returns the startup path by utilizing the special folders enums

Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, pdtName) & ".lnk"), IWshShortcut) - This creates the shortcut from shell, combines the startup path with the product name and adds the ext .lnk to the file shortcut.

myShortcut.TargetPath = Application.ExecutablePath - This sets the shortcuts target path which includes the Executing Path, as well as the file name.

myShortcut.WorkingDirectory = Application.StartupPath - This gets the path of the executing assembly excluding the file name from the path, which only gives you the directory it was launched from.

myShortcut.Description = Descrip - This sets the description on our product for the shortcut.

myShortcut.Save() - Our shortcut is created and now in the folder we specified once this line executes.

Hope this helps somebody looking to create shortcuts programmatically using the Windows Script Host Object Model.

If you feel this is more of a tutorial, feel free to move it there. :)/>/>

This post has been edited by modi123_1: 03 April 2019 - 01:05 PM
Reason for edit:: fixed the code.


Is This A Good Question/Topic? 0
  • +

Replies To: Create Shortcut Programmatically With Windows Script Host Object Model

#2 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Create Shortcut Programmatically With Windows Script Host Object Model

Posted 03 April 2019 - 05:20 PM

I'm sharing a simpler alteration with a tiny minor adjustment. Really all we are going to do is use these two declared variables ::
    Dim PdName As String = Application.ProductName
    Dim pdDiscription As String = "This is text to describe our app"

And then the method we will call is like this ::
    Public Sub CreateSCut()
        Dim ShellSrpt As WshShell = New WshShell()
        Dim startupPath As String = GetFolderPath(SpecialFolder.Startup)
        Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, PdName) & ".lnk"), IWshShortcut)
        myShortcut.TargetPath = Application.ExecutablePath
        myShortcut.WorkingDirectory = Application.StartupPath
        myShortcut.Description = pdDiscription
        myShortcut.Save()
    End Sub

And now you can call the short-cut creation with ::
CreateSCut()


Also, thought I'd elaborate on some other minor bits from the IWshShortcut declaration of myShortcut. You can also set your program's icon by using
myShortcut.IconLocation =

So you could simply add the exectuables files icon to the short-cut link like so ::
myShortcut.IconLocation = myShortcut.TargetPath & ",0"

Of course, you can also add in custom arguments for your app, should you wish to load up a web page or specific parameters for your exe -"param1 here" etc
myShortcut.Arguments = "" 'Any custom arguments used can be passed in here for execution 


Complete you will have something that looks like this with additional properties from the IWshShortcut declaration of myShortcut ::
    Public Sub CreateSCut()
        Dim ShellSrpt As WshShell = New WshShell()
        Dim startupPath As String = GetFolderPath(SpecialFolder.Startup)
        Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, PdName) & ".lnk"), IWshShortcut)
        myShortcut.TargetPath = Application.ExecutablePath
        myShortcut.WorkingDirectory = Application.StartupPath
        myShortcut.Description = pdDiscription
        myShortcut.IconLocation = myShortcut.TargetPath & ",0"
        myShortcut.Arguments = "" 'if you have any...  
        myShortcut.Save()
    End Sub

Looks good, and works well, and no longer displays a default icon. B)

Attached Image

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1