Company logo


Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Creating desktop icons for Visual FoxPro applications

We show you two ways of getting a shortcut to your application onto the user's desktop

This article applies to Visual Foxpro 6.0 and earlier. If you are using a later version, please refer to the note at the end of the article.

If you have ever used Visual FoxPro's Setup Wizard, you will know that this handy tool will cheerfully install a shortcut to your application in the user's Start menu. What the wizard can't do is install a shortcut on the user's desktop. Nor does VFP itself provide any means of creating desktop shortcuts.

Fortunately, there are other ways of achieving that goal. In this article, we will show you two of them. The first is a 'manual' method which you can use in conjuntion with the Setup Wizard. It is easy to implement, provided you have certain information about the user's system. The second technique requires some programming, but is much more generic than the first.

The manual approach

For this first method, you will need to know (i) the path to the directory on the user's system which will hold your application's executable file, and (ii) the path to the user's Desktop directory. This latter directory is usually a sub-directory of the Windows directory. For a typical Windows 9x system with Windows installed on the C drive, the Desktop directory will be c:\windows\desktop, but you will need to check this in each case.

Once you have the required information, follow these steps:

  1. Create a directory on your own computer, with the same path and name as the directory on the user's computer that is to hold your executable program. Place a copy of the executable in this directory.
  2. Create a desktop shortcut on your computer, and make it point to the above copy of the executable. You must perform this step manually, for example by right-clicking on the desktop and selecting New, then Shortcut; then, when prompted for a command line, enter the path and filename of the executable.
  3. Locate the resulting shortcut file in your Desktop directory. The file in question will have the same name as the shortcut, with the extension LNK. Copy this file to the distribution directory that you use with the Setup Wizard.
  4. Write a DOS batch file that will copy the shortcut file from the user's application directory to the user's Desktop directory. The batch file only needs one line, which will be a DOS COPY command. Example:
    COPY "C:\MyApp\MyApp.LNK" "C:\Windows\Desktop"
  5. Place the batch file in the Setup Wizard's distribution directory.
  6. When you run the Setup Wizard, enter the path and name of the batch file as the 'post-setup executable' - this is in Step 4 of the wizard. The path should be the same as that of the application's executable program, and the name should be the name that you gave the batch file, including the BAT extension.

If all goes well, the user will see the shortcut on the desktop as soon as they have run your Setup program. The shortcut will include the application's icon - the one which you specified in the Project Info dialogue - or the default fox icon if you have not specified an application icon. (For more information about specifying application icons, see the Foxstuff article, Do your application icons always show up?).

The generic method

If you prefer a more generic approach - one that does not rely on knowing particular directory names on the target computer - this second technique will be a better choice. It uses the Windows Scripting Host (WSH) to create the shortcut and requires you to write a few lines of Visual FoxPro code.

The drawback of this approach is that it will only work if the WSH is already installed on the user's computer. That won't be a problem for users of Windows 98, Me, NT or 2000, as these systems all have WSH built in. But Windows 95 users will only have it if they have installed Internet Explorer 3.0 or later.

To access the WSH from VFP, you first instantiate it as an object, for example like so:


In this example, oWsh is a variable which will hold an object reference to the WSH.

Th WSH object has a method, called SpecialFolders(), which can be used to determine the name and path of any of the special system folders in Windows. These include Desktop, StartMenu, Favorites, My Documents and Fonts. So to get a path to the Desktop folder, you do this: 

cDeskPath = oWsh.SpecialFolders("desktop")

The WSH object also has a CreateShortcut() method, which does just what its name suggests. This takes as a parameter the name and path of the shortcut which you want to create. The following code will create a shortcut called MyApp.LNK in the Desktop directory:

oShort = ;

After this code is executed, oShort will contain an object reference to the shortcut.

The shortcut object has a TargetPath property which contains the qualified filename of the file to which the shortcut points. The following line of code will make the shortcut point to MyApp.EXE in the c:\MyApp directory:

oShort.TargetPath = "c:\MyApp\MyApp.EXE"

The object also has Description, Hotkey, IconLocation and WorkingDirectory properties which can be set in the same way.

The final step is to save the shortcut, which you do like so: 


After the above code has been executed, the shortcut should appear on the desktop - again using the icon specified in the Project Info dialogue. Using the same general technique, you can create shortcuts in any of the other special folders, or indeed anywhere in the user's file system. You can write the code in your main VFP application, or in a separate utility which can be executed as the post-setup executable in the Setup Wizard.

Note for users of VFP 7.0 and later

Starting with VFP 7.0, the Setup Wizard has been replaced by a limited version of the InstallShield setup program. This program fully supports the installation of desktop shortcuts. Simply click on Shortcuts/Folders in Step 3 (Configuring the Target System). You will see a property sheet in which you can enter the shortcut's description, target path, icon file, working directory and similar details.

Mike Lewis Consultants Ltd. April 2001. Revised August 2001 and November 2001.

Thanks to Michael D. Smith of Doncaster Office Services for corrections to the original version of this article.)

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.