Creating Desktop Shortcuts Using Visual Basic
Pages: 1, 2
WshURLShortcut object is similar to the
WshShortcut object, except that it has only two properties: the
FullName property, which defines the path and name of the Internet shortcut; and the
TargetPath directory, which defines the resource to which the Internet shortcut points. The
WshURLShortcut object also supports the
Load method (which, as in the case of the
WshShortcut method, is private and cannot be called from Visual Basic code) and the
Shortcut Viewer, a downloadable Visual Basic application that allows shortcuts to be created and edited, illustrates the use of the
WshURLShortcut objects. Although we won't present the code here (you can download the file and examine it for yourself), several comments about the code are in order. By default, the utility's File New and File Open dialogs create and retrieve shortcut files found on the user's desktop. Although there are several ways to retrieve this location, the application uses the
SpecialFolders property of the
WshShell object, as follows:
strDesktop = oShell.SpecialFolders("Desktop")
Along with the folder representing the user's desktop items, Windows has a shared folder containing desktop items for all users of a system. Its path can be retrieved as follows:
strDesktop = oShell.SpecialFolders("AllUsersDesktop")
There are also a number of other locations where shortcuts are commonly stored, all of which can be retrieved using the
SpecialFolders property. These include the following:
- The Favorites folder, which is indicated by the string "Favorites".
- The user's Start menu , which is indicated by the string "StartMenu".
- The Start menu of all users, which is indicated by the string "AllUsersStartMenu".
- The users Startup folder (so that the shortcut's target is launched whenever the user logs in to the system), which is indicated by the string "Startup".
- The Startup folder for all users (so that the shortcut's target is launched whenever any user logs in to the system), which is indicated by the string "AllUsersStartup".
- The most recently used (MRU) documents list, which appears when the user selects the Documents item on the Start menu. It is indicated by the string "Recent".
WshShortcut object allows you to select the shortcut file's icon from an executable or DLL containing icon resources, the
WshURLShortcut object does not. (Interestingly, though, the Properties dialog for an Internet Shortcut does allows you to change the icon.) Instead, the location of a default icon for the shortcut is retrieved from the system registry. This location is determined by the default value stored to the HKEY_CLASSES_ROOT\InternetShortcut\DefaultIcon key.
Finally, since the
CreateShortcut method generates an error if we attempt to create or retrieve a shortcut with an extension other than .lnk or .url, it is important that we verify the file extension prior to calling the method. We can do this easily using the
GetExtensionName method of the
FileSystemObject object, which is part of the Microsoft Scripting Runtime Library.
Although it's useful to create shortcuts programmatically from a standard Visual Basic application, it is most common to create desktop shortcuts programmatically from an installation program. By default, the Package and Deployment Wizard bundled with Visual Basic does not allow you to create desktop shortcuts. However, you can easily add this capability by customizing the
Setup1.vbp project used by the wizard. Typically, the wizard's source files are found in C:\Program Files\Microsoft Visual Studio\VB98\Wizards\PDWizard\Setup1, while its executables are found in C:\Program Files\Microsoft Visual Studio\VB98\Wizards\PDWizard\Setup1. Be sure to make a backup copy of the project's files before beginning modification.
Interestingly, the Package and Deployment Wizard features a dynamic link library,
vb6stkit.dll, that includes
fCreateShellLink, a function capable of creating shortcuts. It is intended primarily to add items to program groups or to the Start menu, but it is poorly documented and does not appear to work reliably when creating desktop shortcuts. Rather than relying on it, we'll make use of Windows Script Host and the Scripting Runtime Library. Hence, you should add references to these two libraries to the Setup1 project.
The Package and Deployment Wizard invokes the code responsible for creating a desktop shortcut when the user clicks the Continue button of a form named frmGroup. We should be able to confine our modifications to it.
If the installation program you're creating is going to ask the user whether it should create a desktop shortcut, you'll have to modify the installation interface. In the case of a desktop shortcut, the installation program would need to know whether to create a shortcut only on the user's desktop, or on all users of a system. So, to make this change to the interface, begin by lengthening the form so that it is long enough to accommodate a frame containing two option buttons underneath the lstGroups list box. Move the two command buttons so that they are near the bottom of the form. Next, add the frame and place the two option buttons inside it. Set the following properties:
|Caption||Create Desktop Shortcut|
|First Option Button||Name|
|Caption||for Current User|
|Second Option Button||Name||optAll|
|Caption||for All Users|
The completed form as it appears in Design Mode should look something like the one shown in Figure 1.
Figure 1. Here's how the completed form will appear in Design Mode.
Our code modifications can be confined to the following portion of the
cmdContinue_Click event procedure, which is executed after items are successfully added to a program group:
Else ' ' The group got created ok, so unload Choose Program Group dialog ' and continue on with setup. ' Unload Me End If The following is the complete code block after modifications, with new lines appearing in bold: Else ' Check whether to add desktop shortcut If Me.optCurrent.Value Or Me.optAll.Value Then Dim sDesktop As String, sTarget As String, sAppName As String Dim oShell As New WshShell Dim oShortcut As WshShortcut Dim oFS As New FileSystemObject ' Get name of target sTarget = gstrDestDir & gstrAppExe ' Get Desktop location If Me.optCurrent.Value Then sDesktop = oShell.SpecialFolders("Desktop") Else sDesktop = oShell.SpecialFolders("AllUsersDesktop") End If ' Get root application name sAppName = oFS.GetBaseName(sTarget) ' Form shortcut filename sDesktop = sDesktop & "\" & sAppName & ".lnk" ' Create shortcut Set oShortcut = oShell.CreateShortcut(sDesktop) oShortcut.TargetPath = sTarget oShortcut.Save End If ' ' The group got created ok, so unload Choose Program Group dialog ' and continue on with setup. ' Unload Me End If
Except for the use of some of the global variables defined by the Package and Deployment Wizard, this code is fairly straightforward. We use the
SpecialFolders property of the Shell object to get the location of the user's desktop or of the items common to all users' desktops. We also extract the root filename of the target application by calling the
GetBaseName method. Then it's simply a matter of performing some string concatentation, and creating and saving our shortcut object.
Ron Petrusha is the author and coauthor of many books, including "VBScript in a Nutshell."
Return to WindowsDevCenter.com.
how to create shortcuts
2008-08-04 04:06:30 kalai04 [View]
How to uninstall the shortcut you create
2007-12-20 17:53:44 mARSh [View]
Shortcuts without WindowsScripting Object?
2006-02-24 07:23:59 JRCSystems [View]
Quick and Easy Solution
2005-10-08 06:27:13 cpgguru [View]
Quick and Easy Solution
2005-10-08 06:31:38 cpgguru [View]
2005-06-02 03:15:27 Jooey [View]
2004-04-15 04:36:42 bry [View]
2004-04-14 09:00:37 micampe [View]