Sunday, November 24, 2019

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). Heres 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] URLhttp://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. Heres 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*) Heres a sample usage: //create an .URL file named About Delphi Programming //in the root folder of the C drive //let it point to http://delphi.about.com CreateInterentShortcut(c:\About Delphi Programming.URL , http://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 shortcuts 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] URLhttp://delphi.about.com IconIndex0 IconFileC:\MyFolder\MyDelphiProgram.exe The IconIndex and IconFile fields let you specify the icon for the .URL shortcut. The IconFile could point to your applications 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 programs 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. Heres an example of such an Internet Shortcut: [InternetShortcut] URL file:///c:\MyApps\MySuperDelphiProgram.exe IconIndex 0 IconFile C:\MyFolder\MyDelphiProgram.exe Heres 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.