Reviews

Create an Internet Shortcut (.URL) File Using Delphi

Create an Internet Shortcut (.URL) File Using Delphi

Unlike regular .LNK shortcuts (that point to a document or an application), Internet Shortcuts point to a URL (web document). Here's how to create a .URL file, or Internet Shortcut, using Delphi.

The Internet Shortcut object is used to create shortcuts to Internet sites or web documents. Internet shortcuts are diverse from regular shortcuts (which contain data in a binary file) that point to a document or an application. Such text files with a .URL extension have their content in INI file format.

The easiest way to look inside a .URL file is to open it inside Notepad. The content (in its simplest form) of an Internet Shortcut could look like this:

InternetShortcut
URL=//delphi.about.com

As you can see, .URL files have an INI file format. The URL represents the address location of the page to load. It must specify a fully qualifying URL with the format protocol://server/page

Simple Delphi Function to Create an .URL File

You can easily programmatically create an Internet shortcut if you have the URL of the page to which you want to link. When double-clicked, the default browser is launched and displays the site (or a web document) associated with the shortcut.

Here's a simple Delphi function to create a .URL file. The CreateInterentShortcut procedure creates a URL shortcut file with the provided file name (FileName parameter) for the given URL (LocationURL), overwriting any existing Internet Shortcut with the same name.

uses IniFiles;

procedure CreateInternetShortcut(const FileName, LocationURL : string) ;begin
  with TIniFile.Create(FileName) do
  try
WriteString(
'InternetShortcut',
'URL',
LocationURL) ;
  finally
    Free;
  

end;end; (*CreateInterentShortcut*)

Here's a sample usage:

//create an .URL file named "About Delphi Programming" //in the root folder of the C drive //let it point to //delphi.about.com
CreateInterentShortcut('c:About Delphi Programming.URL ', '//delphi.about.com ') ;

A few notes:

  • You could save a web page as MHT (web archive) and then create a .URL shortcut to be able to access an offline version of a web document.
  • You must provide a full file name, along with the .URL extension, for the FileName parameter.
  • If you already have an Internet Shortcut you are "interested in", you can easily extract the URL from an Internet Shortcut (.url) file.

Specifying the .URL Icon

One of the neater features of the .URL file format is that you can change the shortcut's associated icon. By default the .URL will carry the icon of the default browser. If you want to change the icon, you only have to add two additional fields to the .URL file, as in:

InternetShortcut
URL=//delphi.about.com
IconIndex=0
IconFile=C:MyFolderMyDelphiProgram.exe

The IconIndex and IconFile fields let you specify the icon for the .URL shortcut. The IconFile could point to your application's exe file (IconIndex is the index of the icon as a resource inside the exe).

Internet Shortcut to Open a Regular Document or an Application

Being called an Internet Shortcut, a .URL file format does not permit you to use it for something else-such as a standard application shortcut.

Note that the URL field must be specified in the protocol://server/page format. For example, you could create an Internet Shortcut icon on the Desktop that points to your program's exe file. You only need to specify the "file:///" for the protocol. When you double click on such a .URL file, your application will be executed. Here's an example of such an "Internet Shortcut":

InternetShortcut
URL = file:///c:MyAppsMySuperDelphiProgram.exe
IconIndex = 0
IconFile = C:MyFolderMyDelphiProgram.exe

Here's a procedure that places an Internet Shortcut on the Desktop, the shortcut points to the *current* application. You can use this code to create a shortcut to your program:

uses IniFiles, ShlObj;

function GetDesktopPath: string;//get the location of the Desktop foldervar
DesktopPidl: PItemIDList;
DesktopPath: array 0… MAX_PATH of Char;begin
SHGetSpecialFolderLocation(0, CSIDL_DESKTOP, DesktopPidl) ;
SHGetPathFromIDList(DesktopPidl, DesktopPath) ;
Result := IncludeTrailingPathDelimiter(DesktopPath) ;
end; (*GetDesktopPath*)
procedure CreateSelfShortcut;const
FileProtocol = 'file:///';
var
ShortcutTitle : string;begin
ShortcutTitle := Application.Title + '.URL';
  with TIniFile.Create(GetDesktopPath + ShortcutTitle) do
  try
WriteString(
'InternetShortcut',
'URL',
FileProtocol + Application.ExeName) ;
WriteString(
'InternetShortcut',
'IconIndex',
       '0') ;
WriteString(
'InternetShortcut',
'IconFile',
Application.ExeName) ;
  finally
Free;
  end;end; (*CreateSelfShortcut*)

Note: simply call "CreateSelfShortcut" to create a shortcut to your program on the Desktop.

When to Use .URL

Those handy .URL files will be useful for virtually every project. When you create a setup for your applications, include a .URL shortcut inside the Start menu-let users have the most convenient way to visit your website for updates, examples, or help files.