Solved

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

Posted on 2009-05-20
11
586 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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