Inno Setup to add a path variable
adding in the [Setup] section
Changesenvironment=true
adding in the [Code] section
Procedure setenv (Aenvname, aenvvalue:string; Aisinstall, Aisinsforalluser:boolean)//SET environment variable function
Var
sorgvalue:string;
S1, sFileName, sinsflag:string;
Bretvalue, Binsforalluser:boolean;
Sl:tstringlist;
X:integer;
Begin
Binsforalluser: = Aisinsforalluser;
If Usingwinnt Then
Begin
If Binsforalluser Then
Bretvalue: = Regquerystringvalue (HKEY_LOCAL_MACHINE, ' System/currentcontrolset/control/session Manager/ Environment ', Aenvname, Sorgvalue)
Else
Bretvalue: = Regquerystringvalue (HKEY_CURRENT_USER, ' environment ', Aenvname, Sorgvalue)
Sorgvalue: = Trim (Sorgvalue);
Begin
S1: = Aenvvalue;
If POS (uppercase (Sorgvalue), uppercase (S1) = 0 then//not joined
Begin
If Aisinstall Then
Begin
x: = Length (Sorgvalue);
if (x > 0) and (Stringofchar (Sorgvalue[x], 1) <> '; ') then
Sorgvalue: = Sorgvalue + '; ';
Sorgvalue: = Sorgvalue + S1;
End
End Else
Begin
If not Aisinstall then
Begin
Stringchangeex (Sorgvalue, S1 + '; ', ', True);
Stringchangeex (Sorgvalue, S1, ", True");
End
End
If Binsforalluser Then
Regwritestringvalue (HKEY_LOCAL_MACHINE, ' System/currentcontrolset/control/session Manager/Environment ', AEnvName, Sorgvalue)
Else
Begin
if (not Aisinstall) and (Trim (sorgvalue) = ') Then
Regdeletevalue (HKEY_CURRENT_USER, ' environment ', aenvname)
Else
Regwritestringvalue (HKEY_CURRENT_USER, ' environment ', aenvname, Sorgvalue);
End
End
End else//non-NT system, such as Win98
Begin
SL: = tstringlist.create;
Try
sFileName: = Expandconstant (' {sd}/autoexec.bat ');
Loadstringfromfile (sFileName, S1);
Sl. Text: = S1;
S1: = ' "' + Aenvvalue + '";
S1: = ' Set ' +aenvname + ' =%path%; ' + S1;
Bretvalue: = False;
x: = SL. IndexOf (S1);
If x =-1 then
begin
if Aisinstall then
begin
SL. ADD (S1);
Bretvalue: = True;
end;
End Else//is not added
if not aisinstall Then
begin
SL. Delete (x);
Bretvalue: = True;
end;
If Bretvalue Then
Sl. SaveToFile (sFileName);
Finally
Sl.free;
End
End
End
Procedure curstepchanged (curstep:tsetupstep);//Add environment variable
Begin
If Curstep = Sspostinstall Then
Begin
Setenv (' Path ', Expandconstant (' {app}/package/bpl;{ App}/bin '), true,true); Called here, must be called here, the installation does not need to restart, immediately effective
Setenv (' path ', ' {app}/bin ', true,true);
End
End
Procedure curuninstallstepchanged (curuninstallstep:tuninstallstep);//DELETE environment variable
Begin
Setenv (' Path ', Expandconstant (' {app}/package/bpl;{ App}/bin '), false,true);
Setenv (' path ', ' {app}/bin ', false,true);
End