Inno Setup: Check if file exists anywhere in C: dr

2019-01-27 05:24发布

问题:

Some questions/solutions I found on here were similar but not quite what I needed.

I'm trying to create an installer for a python application I've created for Windows. The installer calls another installer (openscad_installer.exe) and the user has the choice to install that wherever they like (i.e. I don't know the destination and would need to be able to find it) or not to install it at all.

I essentially need to check if the openscad.exe file exists (i.e. if it is installed) anywhere on the computer (in the C: drive) and if it does not exist then I need to uninstall my software.

The uninstall process seems simple enough but I don't know how to find out if the file exists. Thanks for the help.

回答1:

Searching the file in C: drive (and possibly any other drive, as an user may choose to install a software anywhere else) is doable, but can take ages.

I'd suggest you instead check for an existence of the SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OpenSCAD registry key:

const
  OpenSCADRegKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OpenSCAD';

function PrepareToInstall(var NeedsRestart: Boolean): String;
var 
  ResultCode: integer;
begin
  Exec('OpenSCAD-xxx-Installer.exe', '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);

  if RegKeyExists(HKEY_CURRENT_USER_32, OpenSCADRegKey) or
     RegKeyExists(HKEY_CURRENT_USER_64, OpenSCADRegKey) or
     RegKeyExists(HKEY_LOCAL_MACHINE_32, OpenSCADRegKey) or
     RegKeyExists(HKEY_LOCAL_MACHINE_64, OpenSCADRegKey) then
  begin
    Log('OpenSCAD is installed');
  end
    else
  begin
    Log('OpenSCAD is not installed');
    { Abort installation }
    Result := 'OpenSCAD is not installed';
    Exit;
  end;
end;

If you need to know the installation location, read and parse the UninstallString value:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OpenSCAD]
"UninstallString"="C:\\Program Files\\OpenSCAD\\Uninstall.exe"

If you insist on searching for openscad.exe use:

function FindFile(RootPath: string; FileName: string): string;
var
  FindRec: TFindRec;
  FilePath: string;
begin
  Log(Format('Searching %s for %s', [RootPath, FileName]));
  if FindFirst(RootPath + '\*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          FilePath := RootPath + '\' + FindRec.Name;
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
          begin
            Result := FindFile(FilePath, FileName);
            if Result <> '' then Exit;
          end
            else
          if CompareText(FindRec.Name, FileName) = 0 then
          begin
            Log(Format('Found %s', [FilePath]));
            Result := FilePath;
            Exit;
          end;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end
    else
  begin
    Log(Format('Failed to list %s', [RootPath]));
  end;
end;