2005. november 30., szerda

Retrieve a list of all installed applications


Problem/Question/Abstract:

Retrieve a list of all installed applications

Answer:

Under Windows 95, 98, ME, NT and Windows 2000 it is common habit that applications write their installation information into the registry under

HKEY_LOCAL_MACHINE\Software\Mirosoft\Windows\CurrentVersion\UnInstall

Each application has a subkey there and at least defines a display name and an uninstall string.
On my system here I noticed that Allaire Homesite left its installation stamp in HKEY_CURRENT_USER instead of HKEY_LOCAL_MACHINE. So to be save, one might want to scan below HKEY_CURRENT_USER as well. The following sample application retrieves the installed applications and version number - feel free to use it or download it.




{sc-----------------------------------------------------------------------

  -------------------------------------------------------------------
  TForm1.FormCreate                                              9%   5

  Download
-----------------------------------------------------------------------sc}
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Registry;

{$R *.DFM}

{sc-----------------------------------------------------------------------
  Name:       TForm1.FormCreate
  Parameters:
     Sender
  Returns:
     -
  Cyclometric Complexity: 5               ,   3 comments in 32 lines = 9%

  Purpose:
    Retrieve installed apps and collect some info about them

  Date     Coder    CRC Comment
  02/18/01 Tiemann  58  Initial version!
-----------------------------------------------------------------------sc}

procedure TForm1.FormCreate(Sender: TObject);
var
  aList: TStrings;
  i: Integer;
  sVersion: string;
const
  sUninstall = 'Software\Microsoft\Windows\CurrentVersion\UnInstall';
begin
  // enumerate installed applications
  aList := TStringList.Create;

  with TRegistry.Create do
  begin
    RootKey := HKEY_LOCAL_MACHINE;
    if OpenKey(sUninstall, False) then
    begin
      GetKeyNames(aList);
      CloseKey;

      for i := 0 to aList.Count - 1 do
      begin
        if OpenKey(sUninstall +
          '\' +
          aList[i], False) then
        begin
          // collect some info about the installed stuff
          if ValueExists('DisplayVersion') then
            sVersion := 'Version ' + ReadString('DisplayVersion')
          else
            sVersion := '';

          ListBox1.Items.Add(aList[i] + #9 + sVersion);
          CloseKey
        end;
      end;
    end;
    // free the registry object
    Free
  end;
  aList.Free
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése