Solved

how can I create a shortcut in vb.net 2008 express

Posted on 2009-05-20
11
583 Views
Last Modified: 2012-06-22
I have surfed the web for code on creating shortcuts in vb.net but none of them seem to work for me
I always get this "object required error" even though I added the referance and the imports statement probably because I am using vb.net I have included the code
Imports IWshRuntimeLibrary
 

Step 5. Add the following function to your class:
 
 

CODE

Private Function CreateShortCut(ByVal shortcutName As String, ByVal creationDir As String, ByVal targetFullpath As String, ByVal workingDir As String, ByVal iconFile As String, ByVal iconNumber As Integer) As Boolean

  Try

    If Not IO.Directory.Exists(creationDir) Then

      Dim retVal As DialogResult = MsgBox(creationdir & " does not exist. Do you wish to create it?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)

      If retVal = DialogResult.Yes

        IO.Directory.CreateDirectory(creationDir)

      Else

        Return False

      End If

    End If
 

    Dim shortCut As IWshRuntimeLibrary.IWshShortcut

    shortCut = CType(wShell.CreateShortcut(creationDir & "\" & shortcutName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)

    shortCut.TargetPath = targetFullpath

    shortCut.WindowStyle = 1

    shortCut.Description = shortcutName

    shortCut.WorkingDirectory = workingDir

    shortCut.IconLocation = iconFile & ", " & iconNumber

    shortCut.Save()

    Return True

  Catch ex As System.Exception

    Return False

  End Try

End Function

Open in new window

0
Comment
Question by:Dov_B
  • 5
  • 4
  • 2
11 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 350 total points
ID: 24435913
You're missing a line of code in your function:

   Dim wShell As New WshShellClass ' <--- ADD THIS LINE
   Dim shortCut As IWshRuntimeLibrary.IWshShortcut
   ShortCut = CType(wShell.CreateShortcut(creationDir & "\" & shortcutName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)
    shortCut.TargetPath = targetFullpath
0
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24435920
Imports IWshRuntimeLibrary

Private Function CreateShortCut(ByVal shortcutName As String, ByVal creationDir As String, ByVal targetFullpath As String, ByVal workingDir As String, ByVal iconFile As String, ByVal iconNumber As Integer) As Boolean
  Try
    If Not IO.Directory.Exists(creationDir) Then
      Dim retVal As DialogResult = MsgBox(creationdir & " does not exist. Do you wish to create it?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
      If retVal = DialogResult.Yes
        IO.Directory.CreateDirectory(creationDir)
      Else
        Return False
      End If
    End If

    Dim shortCut As IWshRuntimeLibrary.IWshShortcut
    shortCut = CType(wShell.CreateShortcut(creationDir & "\" & shortcutName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)
    shortCut.TargetPath = targetFullpath
    shortCut.WindowStyle = 1
    shortCut.Description = shortcutName
    shortCut.WorkingDirectory = workingDir
    shortCut.IconLocation = iconFile & ", " & iconNumber
    shortCut.Save()
    Return True
  Catch ex As System.Exception
    Return False
  End Try
End Function
0
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24435922
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24436002
@RamanaChoudary...the code and link you posted are also MISSING the line I mentioned above...   =\

Here it is again:

    Dim wShell As New WshShellClass ' <--- ADD THIS LINE
0
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24436029
thanks Idle_Mind
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Dov_B
ID: 24436087
thank you Idlemind and RamanaChoud
I actualy tried Idleminds correction and that got rid of the error
but I still have one problem this code is not mine and I dont understand
the different pieces of info I have to give this function
what is the
shortcutName As String
 creationDir As String,
workingDir As String,
and can I just get rid of the iconfile and iconnumber since I dont know how to get an Icon or perhaps you can tell me how to get on
0
 

Author Comment

by:Dov_B
ID: 24436156
Dear Idlemind and RamanaChoud I think I posted in the middle of your posts could you check my questions above
0
 
LVL 4

Assisted Solution

by:RamanaChoudary
RamanaChoudary earned 150 total points
ID: 24436173
- shortCutName : The name of the shortcut
- creationDir : The folder where the shortcut should be created
- workingDir : The folder to use as working directory for the target of the shortcut
0
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24436183
refer that above link
u can see on bottom of page example too.
how to call above function
0
 

Author Comment

by:Dov_B
ID: 24436568
here is a question
I am having problems running my vb apps from other vb apps because I never know where vb.net is going to install them
so I figured I would have my vb app 1 create a shortcut in the C:\ directory as soon as it runs the first time then I know how to call it fom vb app2 with the following code

System.Diagnostics.Process.Start ("c:\vb app 1 shortcut")
can you give me a hand using the above function to create a shortcut in the C:\ directory when say the form loads
0
 

Author Closing Comment

by:Dov_B
ID: 31583697
dear Idlemind I am having the darndest time trying to run 1 vb.net application from a second vb.net application I just used this program to create a shortcut which I call from a different vb.net app using System.Diagnostics.Process.Start ("c:\newshortcut") and nothing happens
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now