Retreive default browser path

posted 9 Mar 2010, 04:21 by Delphi Basics
There are many different ways to retreive the default browser path.  I will detail two methods.

1. Using registry.
uses
  Registry;

procedure TForm1.Button1Click(Sender: TObject);
var
  Reg: TRegistry;
  KeyName: string;
  ValueStr: string;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CLASSES_ROOT;
    KeyName     := 'htmlfile\shell\open\command';
    if Reg.OpenKey(KeyName, False) then
    begin
      ValueStr := Reg.ReadString('');
      Reg.CloseKey;
      ShowMessage(ValueStr);
    end
    else
      ShowMessage('There isn't a default browser');
  finally
    Reg.Free;
  end;
end;

2. Using associated application.
var
    Path: array[0..255] of char;

begin
  CloseHandle(CreateFile('C:\default.html', GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
  FindExecutable('C:\default.html', nil, Path); //Find the executable (default browser) associated with the html file.
  DeleteFile('C:\default.html');
  Showmessage(Path);
end;



Comments