问题
i need some help with my code. I want to program an installer that will install certain programs by selecting a user. Every user has defined assignments which program should be installed. For now, you have to use the components section to configurate the programs but my goal is to configurate it with an text file. Example for text file could be like:
User1 User2 User3 User4
Program 1 x x x x
Program 2 x x
Program 3 x x
Here is my code:
#define MyAppVersion "1.0"
#define MyAppExeName "Installationssetup.exe"
[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{7A2BAFDB-01B3-4D1C-BB2B-C45A518C47DD}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
DefaultDirName={autopf}\Installation
SourceDir= D:
OutputBaseFilename=Installation-Setup
WizardStyle= classic
DisableWelcomePage= no
[Types]
Name: "User 1"; Description: "User 1"
Name: "User 2"; Description: "User 2"
Name: "User 3"; Description: "User 3"
Name: "User 4"; Description: "User 4"
Name: "Custom"; Description: "Custom"
Name: "Random"; Description:"Random"; Flags: iscustom
[Components]
Name: "Select"; Description: "Alle auswählen:"; Types: User 1 User 2 User 3 User 4
Name: "Select\Program_1"; Description: "Program_1"; Types: User 1 User 2 User 3 User 4
Name: "Select\Program_2"; Description: "Program_2"; Types: User 2 User 4
Name: "Select\Program_3"; Description: "Program_3"; Types: User 1 User 3
[Files]
Source: "TEST \Software\x64\Program_1"; DestDir: "{app}\Program_1"; Flags: ignoreversion recursesubdirs; Components: Select\Program_1;
Source: "TEST \Software\x64\Program_2"; DestDir: "{app}\Program_2"; Flags: ignoreversion recursesubdirs; Components: Select\Program_2;
Source: "TEST \Software\x64\Program_3"; DestDir: "{app}\Program_3"; Flags: ignoreversion recursesubdirs; Components: Select\Program_3;
[Code]
var
TypesPage: TWizardPage;
User 1_Button: TNewRadioButton;
User 2_Button: TNewRadioButton;
User 4_Button: TNewRadioButton;
User 3_Button: TNewRadioButton;
Custom_Button: TNewRadioButton;
procedure InitializeWizard();
begin
{ Create custom "types" page }
TypesPage := CreateInputOptionPage(wpSelectDir,
'Select User', '' ,
'Please select the right User',true,false);
User 1_Button := TNewRadioButton.Create(TypesPage);
User 1_Button.Parent := TypesPage.Surface;
User 1_Button.Caption := 'User 1';
User 1_Button.Top := 50;
User 1_Button.Height := ScaleY(User 1_Button.Height);
User 1_Button.Checked := (WizardForm.TypesCombo.ItemIndex = 0);
User 2_Button := TNewRadioButton.Create(TypesPage);
User 2_Button.Parent := TypesPage.Surface;
User 2_Button.Caption := 'User 2';
User 2_Button.Height := ScaleY(User 2_Button.Height);
User 2_Button.Top := User 1_Button.Top + User 1_Button.Height + ScaleY(16);
User 2_Button.Checked := (WizardForm.TypesCombo.ItemIndex = 1);
User 3_Button := TNewRadioButton.Create(TypesPage);
User 3_Button.Parent := TypesPage.Surface;
User 3_Button.Caption := 'User 3';
User 3_Button.Height := ScaleY(User 3_Button.Height);
User 3_Button.Top := User 2_Button.Top + User 2_Button.Height + ScaleY(16);
User 3_Button.Checked := (WizardForm.TypesCombo.ItemIndex = 2);
User 4_Button := TNewRadioButton.Create(TypesPage);
User 4_Button.Parent := TypesPage.Surface;
User 4_Button.Caption := 'User 4';
User 4_Button.Height := ScaleY(User 4_Button.Height);
User 4_Button.Top := User 3_Button.Top + User 3_Button.Height + ScaleY(16);
User 4_Button.Checked := (WizardForm.TypesCombo.ItemIndex = 3);
Custom_Button := TNewRadioButton.Create(TypesPage);
Custom_Button.Parent := TypesPage.Surface;
Custom_Button.Caption := 'Custom';
Custom_Button.width := 200;
Custom_Button.Height := ScaleY(Custom_Button.Height);
Custom_Button.Top := User 4_Button.Top + User 4_Button.Height + ScaleY(16);
Custom_Button.Checked := (WizardForm.TypesCombo.ItemIndex = 4);
WizardForm.TypesCombo.Visible :=False; //Dropdown List removed
WizardForm.IncTopDecHeight(WizardForm.ComponentsList,
-(WizardForm.ComponentsList.Top-WizardForm.TypesCombo.Top));
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
if CurPageID = TypesPage.ID then
begin
if User 1_Button.Checked then WizardForm.TypesCombo.ItemIndex :=0
else
if User 2_Button.Checked then WizardForm.TypesCombo.ItemIndex := 1
else
if User 3_Button.Checked then WizardForm.TypesCombo.ItemIndex := 2
else
if User 4_Button.Checked then WizardForm.TypesCombo.ItemIndex := 3
else
if Custom_Button.Checked then WizardForm.TypesCombo.ItemIndex := 4;
WizardForm.TypesCombo.OnChange(WizardForm.TypesCombo);
end;
Result:= true;
end;
回答1:
As you do not seem to care about the configuration file format, let's pick INI file, as Inno Setup has functions to parse it:
[Users]
user1=Program1,Program3
user2=Program1,Program2
user3=Program1,Program3
user4=Program1,Program2
Then the following script will do:
[Types]
Name: "user1"; Description: "User 1"
Name: "user2"; Description: "User 2"
Name: "user3"; Description: "User 3"
Name: "user4"; Description: "User 4"
[Files]
Source: "TEST \Software\x64\Program_1"; DestDir: "{app}\Program_1"; \
Flags: ignoreversion recursesubdirs; Check: ShouldInstallProgram('Program1')
Source: "TEST \Software\x64\Program_2"; DestDir: "{app}\Program_2"; \
Flags: ignoreversion recursesubdirs; Check: ShouldInstallProgram('Program2')
Source: "TEST \Software\x64\Program_3"; DestDir: "{app}\Program_3"; \
Flags: ignoreversion recursesubdirs; Check: ShouldInstallProgram('Program3')
[Code]
function ShouldInstallProgram(ProgramName: string): Boolean;
var
UserName: string;
ProgramsStr: string;
Programs: TStringList;
begin
UserName := WizardSetupType(False);
ProgramsStr :=
GetIniString('Users', UserName, '', ExpandConstant('{src}\UserPrograms.ini'));
Programs := TStringList.Create;
Programs.CommaText := ProgramsStr;
Result := (Programs.IndexOf(ProgramName) >= 0);
Programs.Free;
end;
Type names must be lowercase for this to work. And casing of program names matter.
As the code now actually does not use the [Types] at all, you can replace the WizardSetupType with direct check to your custom page selection. And you can remove the redundant [Types] section and your NextButtonClick event function.
来源:https://stackoverflow.com/questions/62891994/inno-setup-how-to-find-string-in-text-file-so-that-the-installer-knows-which-p