运行安装程序时,有什么方法可以从 PascalScript 访问文件列表([Files] 部分中的条目)?我们正在尝试使应用程序可以直接从设置运行,而不必安装它,这将使维护文件列表更容易。
最佳答案
这里的想法是将文件名存储到单独的文本文件中(这里是 Source.txt
),其中每一行都是一个文件。然后预处理器将为您生成脚本。实际上它创建了一个数组,其中包含来自 Source.txt
的文件列表。并将其所有元素添加到 [Files]
部分并在 [Code]
部分它将填充字符串列表(这里使用列表框来显示内容)。
重要提示:
在 Source.txt
的末尾必须有一个额外的非空行文件,所以只需添加例如;
在文件的末尾。
脚本:
#define FilesSource "d:\Source.txt"
#define FileLine
#define FileIndex
#define FileCount
#define FileHandle
#dim FileList[65536]
#sub ProcessFileLine
#if FileLine != ""
#expr FileList[FileCount] = FileLine
#expr FileCount = ++FileCount
#endif
#endsub
#for {FileHandle = FileOpen(FilesSource); \
FileHandle && !FileEof(FileHandle); \
FileLine = FileRead(FileHandle)} \
ProcessFileLine
#if FileHandle
#expr FileClose(FileHandle)
#endif
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Files]
#sub AddFileItem
#emit 'Source: "' + FileList[FileIndex] + '"; DestDir: "{app}"'
#endsub
#for {FileIndex = 0; FileIndex < FileCount; FileIndex++} AddFileItem
[Code]
procedure InitializeWizard;
var
FileList: TStringList;
FileListBox: TListBox;
CustomPage: TWizardPage;
begin
CustomPage := CreateCustomPage(wpWelcome, 'Theme selection page', '');
FileListBox := TListBox.Create(WizardForm);
FileListBox.Parent := CustomPage.Surface;
FileListBox.Align := alClient;
FileList := TStringList.Create;
try
#sub AddFileItemCode
#emit ' FileList.Add(''' + FileList[FileIndex] + ''');'
#endsub
#for {FileIndex = 0; FileIndex < FileCount; FileIndex++} AddFileItemCode
FileListBox.Items.Assign(FileList);
finally
FileList.Free;
end;
end;
#expr SaveToFile("d:\PreprocessedScript.iss")
测试源.txt:
MyProg.exe
MyProg.chm
Readme.txt
;
测试 PreprocessedScript.iss 的输出:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"
Source: "Readme.txt"; DestDir: "{app}"
[Code]
procedure InitializeWizard;
var
FileList: TStringList;
FileListBox: TListBox;
CustomPage: TWizardPage;
begin
CustomPage := CreateCustomPage(wpWelcome, 'Theme selection page', '');
FileListBox := TListBox.Create(WizardForm);
FileListBox.Parent := CustomPage.Surface;
FileListBox.Align := alClient;
FileList := TStringList.Create;
try
FileList.Add('MyProg.exe');
FileList.Add('MyProg.chm');
FileList.Add('Readme.txt');
FileListBox.Items.Assign(FileList);
finally
FileList.Free;
end;
end;
关于inno-setup - 通过 InnoSetup 中的脚本访问文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924137/