List all web-browsers installed on a Windows machine

前端 未结 2 1696
傲寒
傲寒 2021-02-10 00:46

Is there a common method/api to list all web browsers (name, executable, default yes/no) installed on my machine (and per user), and how to find out which is t

2条回答
  •  失恋的感觉
    2021-02-10 01:27

    You could do something like

    procedure ListRegisteredBrowsers(List: TStrings);
    var
      reg: TRegistry;
      ki: TRegKeyInfo;
      i: Integer;
      keyname: string;
      len: DWORD;
    begin
      reg := TRegistry.Create;
      try
        reg.RootKey := HKEY_LOCAL_MACHINE;
        if not Reg.KeyExists('\SOFTWARE\Clients\StartMenuInternet') then Exit;
        if not Reg.OpenKey('\SOFTWARE\Clients\StartMenuInternet', false) then
          raise Exception.Create('ListRegisteredBrowsers: Could not open registry key.');
        if not reg.GetKeyInfo(ki) then
          raise Exception.Create('ListRegisteredBrowsers: Could not obtain registry key information.');
        List.Clear;
        SetLength(keyname, len);
        for i := 0 to ki.NumSubKeys - 1 do
        begin
          len := ki.MaxSubKeyLen + 1;
          if RegEnumKeyEx(reg.CurrentKey, i, PChar(keyname), len, nil, nil, nil, nil) <> ERROR_SUCCESS then
            RaiseLastOSError;
          if reg.OpenKey('\SOFTWARE\Clients\StartMenuInternet\' + keyname, false) then
            List.Add(reg.ReadString(''));
          Reg.OpenKey('\SOFTWARE\Clients\StartMenuInternet', true);
        end;
      finally
        reg.Free;
      end;
    end;
    

    and

    function GetDefaultBrowser: string;
    var
      reg: TRegistry;
    begin
      result := '';
      reg := TRegistry.Create;
      try
        reg.RootKey := HKEY_CURRENT_USER;
        if Reg.OpenKey('\SOFTWARE\Clients\StartMenuInternet', false) then
          result := reg.ReadString('')
        else
        begin
          reg.RootKey := HKEY_LOCAL_MACHINE;
          if Reg.OpenKey('\SOFTWARE\Clients\StartMenuInternet', false) then
            result := reg.ReadString('')
        end;
        reg.RootKey := HKEY_LOCAL_MACHINE;
        if Reg.OpenKey('\SOFTWARE\Clients\StartMenuInternet\' + result, false) then
          result := reg.ReadString('');
      finally
        reg.Free;
      end;
    end;
    

    Test it:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      sl: TStringList;
      i: Integer;
      DefBrw: string;
    begin
      DefBrw := GetDefaultBrowser;
      sl := TStringList.Create;
      try
        ListRegisteredBrowsers(sl);
        Memo1.Lines.BeginUpdate;
        for i := 0 to sl.Count - 1 do
          if SameText(sl[i], DefBrw) then
            Memo1.Lines.Add(sl[i] + ' (Default)')
          else
            Memo1.Lines.Add(sl[i]);
        Memo1.Lines.EndUpdate;
      finally
        sl.Free;
      end;
    end;
    

提交回复
热议问题