Home » Azure

Create a .lnk file with arguments

Using this code I can create a shortcut to my application,

Dim shell As New WshShell
    Dim shortcut As WshShortcut = shell.CreateShortcut(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Link To My Application.lnk")
    shortcut.TargetPath = Application.ExecutablePath
    shortcut.IconLocation = Application.ExecutablePath
    shortcut.Description = "My Application Name"

But how can I create a shortcut with arguments.    If i put the arguments in the shortcut.TargetPath i get a error.


Any Help?


9 Answers Found


Answer 1

The code  you posted does in code what you can do by right-clicking on the target file  and selecting "Create Shortcut".  How do you manually create  a shortcut  with arguments?

Answer 2

Right click the target file  -> Create Shortcut -> Right Click Shortcut -> Click Properties -> Place Arguments at the end of the target.

Answer 3

Without wading through help  files:  ReadAllBytes-->Find the target-->add arguments-->WriteAllBytes.


Answer 5

I used the above link  and got this code
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
     Create_ShortCut(Application.ExecutablePath, My.Computer.FileSystem.SpecialDirectories.Desktop, PageTitleLabel.Text, "/url=" & WebBrowserURL.Text, "", IWshRuntimeLibrary.WshWindowStyle.WshNormalNoFocus, Application.ExecutablePath)
  End Sub
  Private Sub Create_ShortCut(ByVal sTargetPath As String, ByVal sShortCutPath As String, ByVal sShortCutName As String, _
              Optional ByVal sArguments As String, _
              Optional ByVal sWorkPath As String, _
              Optional ByVal eWinStyle As WshWindowStyle = vbNormalFocus, Optional ByVal iIconNum As Integer)
    ' Requires reference to Windows Script Host Object Model
    Dim oShell As IWshRuntimeLibrary.WshShell
    Dim oShortCut As IWshRuntimeLibrary.WshShortcut

    oShell = New IWshRuntimeLibrary.WshShell
    oShortCut = oShell.CreateShortcut(My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & PageTitleLabel.Text & ".lnk")
    With oShortCut
      .TargetPath = Application.ExecutablePath
      .Arguments = "/url=http://www.ifxsound.net"
      .WorkingDirectory = Application.ExecutablePath
      .IconLocation = Application.ExecutablePath
    End With

    oShortCut = Nothing : oShell = Nothing
  End Sub
And everything works.   Except one tiny problem.. I get a "Optional parameters must specify a default value." Error at the comma at 
Optional ByVal sArguments As String, _
What do I do?

Answer 6

When you declare parameter as optional, you must assign default value


Optional ByVal sArguments As String should be like Optional ByVal sArguments As String="optional"




Answer 7

put  everything together and got this error


System.Runtime.InteropServices.COMException was unhandled


  Message="Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))"




Answer 8

put  everything together and got this error



System.Runtime.InteropServices.COMException was unhandled


  Message="Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))"




Did you add reference to IWshRuntimeLibrary COM interop to access Windows Script Host (WSH) Object?  if not, then try to add reference by right click reference from your project solution explorer, select add reference, when reference window pop up, select COM TAB and scroll down to Windows Script Host Object Model and select it, then click Ok.

So add this below with others namespace

Imports IWshRuntimeLibrary



Answer 9

I have already added that reference to my application  and it does say Imports IWshRuntimeLibrary at the top.


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure