Solved

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

Posted on 2009-05-20
11
588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 86

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

687 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