2006. szeptember 14., csütörtök

Adding an url to Browser/Windows Favorite


Problem/Question/Abstract:

How to add an url to Browser/Windows Favorite

Answer:

Note. I've made this example for complete beginner.
Jump to the code if you don't need any explanation. There no API manipulation here, we place manually the shortcut in the the folder.

Here's how you can add an url in you favorite. This work for any browser, except if they use special data type to store their url library. The only example I see of an application that use another type of data is the utility LinkMan wich is, anyway, not a browser application.

Basicly, to create a shortcut url all you need to know is the structure of this file type. You need also to know a little about file manipulation, if you don't, this example will serve also as a guide for very simple file manipulation.

First put 3 TEdit component on your form and add TButton component.
Add three label to identify you EditBox. First EditBox should be associate with Folder, the second with URL and the last with Title.

Folder -  Will store the access path to your browser favorite folder
URL - Will store the URL (Http, www, etc)
Title - Is the name you give to the URL (Yahoo, Baltsoft)

For the AddUrl procedure to work, you need to pass it these three paramater as string. To do so affect your .Text property of your TEdit Component to your own variables.

If you are new to Delphi you will find pretty interesting the output to file part where I use AssignFile, ReWrite, WriteLn... I suggest you try some of you own app using those command to test results with your own value.  

I included also a small line that can create a folder if the folder don't exist. This is a proper to the use of FileCtrl. Also of interest the small handling of \ if it's not present for lazy user.

Don't change you components name, it will be easier to follow the code.

Here we go..

unit MyUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
    FileCtrl;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure AddUrl(Folder, Url, Title: string);
  end;

var
  Form1: TForm1;
  MyFolder: string;
  MyUrl: string;
  MyTitle: string;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyFolder := Edit1.Text;
  MyURL := Edit2.Text;
  MyTitle := Edit3.Text;
  AddUrl(MyFolder, MyUrl, MyTiTle);
end;

procedure TForm1.AddURL(Folder, Url, Title: string);
var
  MyUrlFile: TextFile;
begin
  if Folder[Length(Folder)] <> '\' then
    Folder := Folder + '\';
  if not DirectoryExists(Folder) then
    ForceDirectories(Folder);
  try
    AssignFile(MyUrlFile, Folder + title + '.url');
    Rewrite(MyUrlFile);
    WriteLn(MyUrlFile, '[InternetShortcut]');
    WriteLn(MyUrlFile, 'URL=' + url);
  finally
    Closefile(MyUrlFile);
  end;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése