如何为以前的Inno安装程序安装自动设置DefaultDirName?

时间:2011-04-19 17:48:24

标签: inno-setup

我在Inno Setup中的先前安装(A)的AppID = {{8ADA0E54-F327-4717-85A9-9DE3F8A6D100}。

我有另一个具有不同AppID的安装(B),我想将它安装到与安装(A)相同的目录中。

如何自动获取DefaultDirName?我不想使用相同的AppID,因为当我卸载安装(B)并且安装(A)保持安装时,它将从注册表中删除AppID字符串(安装(A)字符串)。

请帮帮我吗?

2 个答案:

答案 0 :(得分:6)

您可能需要一些代码来执行您想要的操作。您还需要一种方法来查找Application A的安装目录。这是我使用过的一些代码

[Setup]
DefaultDirName={code:GetDefaultDir}

[Code]
function GetDefaultDir(def: string): string;
var
sTemp : string;
begin
    //Set a defualt value so that the install doesn't fail.  
    sTemp := ExpandConstant('{pf}') + '\MyCompany\MyAppA';

    //We need to get the current install directory.  
    if RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\MyCompany\Products\MyAppNameA',
     'InstallDir', sTemp) then
     begin
    //We found the value in the registry so we'll use that.  Otherwise we use the default 
     end;
    Result := sTemp;
end;

答案 1 :(得分:4)

我开发了以下代码,以便根据AppID查找安装目录。它适用于每个用户的注册表项以及整个计算机的注册表项。它已在Windows 7 Enterprise上的域和Virtual PC XP Professional机器上进行了测试:

[code]
const
   PreviousAppID = '8ADA0E54-F327-4717-85A9-9DE3F8A6D100';
   AppFolder     = 'SomeFolder';

   UninstallPath = 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{'
                  + PreviousAppID + '}_is1';

   // Some posts have 'InstallDir', but I have never observed that
   InstallKey    = 'InstallLocation';

function GetDefaultDir( Param: String ) : String;
var
   UserSIDs: TArrayOfString;
   I:        Integer;

begin
   // Check if the current user installed it
   if RegQueryStringValue( HKEY_CURRENT_USER, UninstallPath,
                           InstallKey, Result ) then

   // Current user didn't install it.  Did someone else?
   else if RegGetSubkeyNames( HKEY_USERS, '', UserSIDs ) then begin
      for I := 0 to GetArrayLength( UserSIDs ) - 1 do begin
         if RegQueryStringValue( HKEY_USERS, UserSIDs[I] + '\' + UninstallPath,
                                 InstallKey, Result ) then break;
      end;  
   end;

   // Not installed per-user
   if Result = '' then begin
      // What about installed for the machine?
      if RegQueryStringValue( HKEY_LOCAL_MACHINE, UninstallPath,
                              InstallKey, Result ) then

      // Doesn't appear to be installed, as admin default to Program Files
      else if IsAdminLoggedOn() then begin
         Result := ExpandConstant('{pf}\') + AppFolder;

      // As non-admin, default to Local Application Data
      end else begin
         Result := ExpandConstant('{localappdata}\') + AppFolder;
      end;
   end;
end;