inno-setup - 通过 InnoSetup 中的脚本访问文件列表

标签 inno-setup pascalscript

运行安装程序时,有什么方法可以从 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/

相关文章:

windows - 如何在 Inno Setup 中使用 Pascal 变量?

inno-setup - 如何在 Inno Setup 中安装期间为可选文件添加复选框?

installation - 从 inno 设置中的底部面板中删除语言标签

inno-setup - 如何更改 Inno Setup DisplayName 卸载条目

inno-setup - Inno Setup,WizardImageFile 即使是 100% DPI 也被拉伸(stretch)

inno-setup - MsgBox - 制作不可点击的确定按钮并更改为倒计时 - Inno Setup

inno-setup - 在代码部分中递归地设置Inno Setup : copy folder,子文件夹和文件

inno-setup - 我的 Inno Setup 脚本创建了两个桌面图标

inno-setup - Inno Setup 语法 - OR, AND

inno-setup - TNewCheckListBox 选中未更新