Thursday, September 10, 2009

Add or Remove Programs is too freaking slow?

I really hate Windows Add or Remove Programs it takes so much time that I freak out.
Why it takes so much time? I believe it gathers information about every program, like: when was the last time started, how many times, who used the program, bla bla bla.
Anyways here's a screenshot of my dUninstaller in action



The implementation is very simple, reads some registry keys and shows you the information almost like "Add or Remove Programs" does.
So all you need is a constant
const
  UNINSTALLROOT = '\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\';

and then a way to display the info in a TListView from the Win32 palette
procedure TfrmMain.btnRefreshClick(Sender: TObject);
var
  theRegistry: TRegistry;
  uEntries: TStrings;
  index: Integer;
  item: TListItem;
begin
  uList.Clear;
  uEntries := TStringList.Create;
  theRegistry := TRegistry.Create;
  theRegistry.RootKey := HKEY_LOCAL_MACHINE;
  theRegistry.OpenKey(UNINSTALLROOT, False);
  theRegistry.GetKeyNames(uEntries);
  theRegistry.CloseKey;

  for index := 0 to uEntries.Count -1 do begin
    theRegistry.OpenKey(UNINSTALLROOT + uEntries[index], False);
    if theRegistry.ValueExists('DisplayName') then begin
      item := uList.items.add;
      item.Caption := theRegistry.ReadString('DisplayName');
      if theRegistry.ValueExists('DisplayVersion') then
        item.SubItems.Add(theRegistry.ReadString('DisplayVersion')) else
        item.SubItems.Add('null');
      if theRegistry.ValueExists('Publisher') then
        item.SubItems.Add(theRegistry.ReadString('Publisher')) else
        item.SubItems.Add('null');
      if theRegistry.ValueExists('UninstallString') then
        item.SubItems.Add(theRegistry.ReadString('UninstallString')) else
        if theRegistry.ValueExists('QuietUninstallString') then
          item.SubItems.Add(theRegistry.ReadString('QuietUninstallString')) else
          item.SubItems.Add('null');
    end;
    theRegistry.CloseKey;
  end;

  FreeAndNil(theRegistry);
  FreeAndNil(uEntries);
  StatusBar.SimpleText := Format('%d Application(s)', [uList.Items.Count]);
end;

now to uninstall a program you must select it form the list and when you press the Uninstall button it triggers this event
procedure TfrmMain.btnUninstallClick(Sender: TObject);
var
  sUninstall: String;
begin
  if uList.ItemIndex = -1 then
    Exit;
  sUninstall := uList.ItemFocused.SubItems[2];
  if sUninstall <> 'null' then begin
    WinExec(PChar(sUninstall), SW_SHOWDEFAULT);
    btnRefresh.Click;
  end;
end;

And that's about it, you download the application or the source code if you wish.

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)