Create Desktop Shortcuts &
Refresh the Desktop
This sample shows how to create Desktop Shortcuts or
Shell Links from Visual Basic using only standard
API functions. It does not rely on any typelib files, external .DLL's or
.OCX's. Although this program only creates Desktop shortcuts, with a minor code
change you can create the shortcut in any folder.
Also shown is how to determine the location Special Folders
such as the Desktop and Recent Files folder and perform file operations using
the SHFileOperation API. The Designed for Windows
XP Application Specification says that you should get the path to
Special Folders using the SHGetFolderPath API. To
learn about this API, see my Special Folders sample.
Another very valuable technique illustrated is how to refresh the Desktop which
is especially useful if you are running an Active Desktop.
The downside to this sample is that it is somewhat limited since it does not let
you set shortcut properties like the Start In folder, Run Mode or Target String
arguments. For full control over these properties, see one of my other Shell
Download Source Code
The basic approach used is to create a Link (.Lnk)
file in the Recent Files folder, move the file to
the Desktop folder, then tell Windows to refresh
the Desktop so the shortcut appears.
The first step is determining the locations of the Desktop
and Recent Files folders. In Windows speak, such
folders are file system objects called Special Folders
and are referenced by Item Identifiers. To find
their locations the SHGetSpecialFolderLocation API
is provided. This function takes 3 parameters. The first is the handle to the
calling window. The next is a flag specifying the folder to locate.
CSIDL_DESKTOPDIRECTORY and CSIDL_RECENT
are used to locate the Desktop and Recent Files folders, respectively. The last
value is an ITEMIDLIST structure which gets
populated with a list of item identifiers. Once the item identifier is obtained
for the desired folder it must be converted to a path. A call to
SHGetPathFromIDList does this.
The path of the application that we are creating a shortcut for is passed to the
SHAddToRecentDocs API to add it to the Recent Files folder. Once
there, a ".Lnk" is appended to its name and it is copied to the Desktop with
SHFileOperation (for details on this function see my
file operations page). Now the .Lnk file is renamed to the name
specified by the user. Again, SHFileOperation is
All that remains is to refresh the desktop to insure the shortcut appears.
SHGetSpecialFolderLocation is called one more time to get the
location of the Desktop. This differs from the desktop folder used earlier. In
this case the CSIDL_DESKTOP flag is passed. Armed
with the desktop's location a notification message is sent to Windows via
SHChangeNotify along with the item identifier of the desktop. Upon
receipt of this notification Windows refreshes the desktop and our short cut
Download the project and press F5 to run the program. Enter the full path of the
application to create a shortcut for and a name for the shortcut then click the