Inno setup制作安装包有三个文件夹根据自定义页面的选择判断是否复制某个文件夹中的所有文件到某个位置
你可以使用Inno Setup的CurPageChanged事件和WizardForm.NextButton.OnClick事件来实现根据自定义页面的选择来复制文件夹中的文件。
首先,在CurPageChanged事件中添加以下代码:
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpSelectComponents then begin
// 根据自定义页面的选择来决定是否显示目标文件夹选择页面
if IsComponentSelected('CopyFiles') then
WizardForm.SelectDirPage.Visible := True
else
WizardForm.SelectDirPage.Visible := False;
end;
end;
然后,在WizardForm.NextButton.OnClick事件中添加以下代码:
procedure NextButtonClick(CurPageID: Integer; var Done: Boolean);
var
SourceDir: String;
DestDir: String;
begin
if CurPageID = wpSelectDir then begin
// 获取选择的源文件夹和目标文件夹
SourceDir := ExpandConstant('{app}\SourceFolder');
DestDir := WizardForm.DirEdit.Text;
// 复制文件夹中的所有文件到目标文件夹
if DirExists(SourceDir) then
if not CopyDir(SourceDir, DestDir, False) then
MsgBox('无法复制文件夹中的文件。', mbError, MB_OK);
end;
end;
在上述代码中,CopyFiles是自定义页面中的一个组件,用于选择是否复制文件夹中的文件。CopyDir是一个自定义函数,用于复制文件夹中的所有文件到目标文件夹。你需要在脚本中实现这个函数,例如:
function CopyDir(const SourceDir, DestDir: String; const CopySubdirs: Boolean): Boolean;
var
FindRec: TFindRec;
SourceFilePath: String;
DestFilePath: String;
begin
Result := True;
if FindFirst(SourceDir + '\*', FindRec) = 0 then begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then begin
SourceFilePath := SourceDir + '\' + FindRec.Name;
DestFilePath := DestDir + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
Result := Result and FileCopy(SourceFilePath, DestFilePath, False)
else if CopySubdirs then begin
if not DirExists(DestFilePath) then
CreateDir(DestFilePath);
Result := Result and CopyDir(SourceFilePath, DestFilePath, CopySubdirs);
end;
end;
until FindNext(FindRec) <> 0;
finally
FindClose(FindRec);
end;
end;
end;
这样,根据自定义页面的选择,当用户点击下一步按钮时,如果选择了复制文件夹中的文件的选项,将会复制文件夹中的所有文件到目标文件夹中
原文地址: https://www.cveoy.top/t/topic/hXB8 著作权归作者所有。请勿转载和采集!