שתף באמצעות


Create a .lnk file with arguments

Question

Wednesday, July 7, 2010 8:38 PM

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"
    shortcut.Save()

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

 

Any Help?

All replies (10)

Monday, July 12, 2010 10:35 PM ✅Answered

I was able to fix all the problems and create a shortcut with Arguments using this code:

Dim sDesktop As String, sTarget As String, sAppName As String
    Dim oShell As New WshShell
    Dim oShortcut As WshShortcut
    Dim oFS As New FileSystemObject
    Dim Arguments As String
    ' Get name of target
    sTarget = Application.ExecutablePath

    ' Get Desktop location
    sDesktop = My.Computer.FileSystem.SpecialDirectories.Desktop

    ' Get root application name
    sAppName = oFS.GetBaseName(sTarget)

    ' Form shortcut filename
    sDesktop = sDesktop & "\" & sAppName & ".lnk"

    ' Create Arguments
    Arguments = 'Some Awesome Arguments

    ' Create shortcut
    oShortcut = oShell.CreateShortcut(sDesktop)
    oShortcut.TargetPath = sTarget
    oShortcut.Arguments = Arguments
    oShortcut.Save()

From: http://oreilly.com/pub/a/windows/2004/04/13/VB_Shortcuts.html


Wednesday, July 7, 2010 9:17 PM

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?


Wednesday, July 7, 2010 9:31 PM

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


Wednesday, July 7, 2010 10:01 PM

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


Wednesday, July 7, 2010 10:52 PM

What if I tried this? http://www.vbforums.com/showpost.php?p=2637397&postcount=3


Wednesday, July 7, 2010 11:08 PM

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
      .Save()
    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?


Wednesday, July 7, 2010 11:32 PM

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

So

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

 

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Thursday, July 8, 2010 7:35 PM

I put everything together and got this error

 

System.Runtime.InteropServices.COMException was unhandled

  ErrorCode=-2147352567

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

  Source="Interop.IWshRuntimeLibrary"

 


Thursday, July 8, 2010 9:43 PM

I put everything together and got this error

 

 

System.Runtime.InteropServices.COMException was unhandled

  ErrorCode=-2147352567

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

  Source="Interop.IWshRuntimeLibrary"

 

 

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

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Friday, April 28, 2017 3:31 PM

I found the answer to this. It should be declared as such

Dim shortcut As WshRuntimeLibrary.WshShortcut = Shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.desktop) & "LinkToMyApplication.lnk")