如何使用inno安装程序安装程序检查硬盘中是否有可用空间来安装应用程序

时间:2014-02-14 08:59:41

标签: inno-setup

我已经构建了一个使用inno setup安装应用程序的安装程序。但是我想显示一条错误消息,表明如果没有可用空间,我将要安装应用程序的驱动器或路径中没有足够的空间。默认情况下,当硬盘或选定路径中没有可用空间时,我正在构建能够显示消息的能力。但它显示YES和NO按钮继续或取消。在这里,我想用OK按钮显示错误消息,当用户单击确定按钮时,它应该停止安装。请帮我解决这个问题。我找不到任何方法。 我调用了GetSpaceOnDisk64()和GetSpaceOnDisk(),它们都无法正常工作并抛出异常

我收到错误。请查看附件中的图片enter image description here

2 个答案:

答案 0 :(得分:4)

要确定特定文件夹的驱动器上的可用空间(在您的情况下是所选目录),您可以调用GetSpaceOnDiskGetSpaceOnDisk64功能。它们之间的区别在于第一个能够返回以字节为单位的空间信息以及以兆字节为单位的空间信息。后者以字节为单位返回此信息。对于以下示例,我选择了第一个提到的函数,因此您可以通过修改单个布尔参数来决定要操作的单位:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
procedure ExitProcess(uExitCode: UINT);
  external 'ExitProcess@kernel32.dll stdcall';

function IsEnoughFreeSpace(const Path: string; MinSpace: Cardinal): Boolean;
var
  FreeSpace, TotalSpace: Cardinal;
begin
  // the second parameter set to True means that the function operates with
  // megabyte units; if you set it to False, it will operate with bytes; by
  // the chosen units you must reflect the value of the MinSpace paremeter
  if GetSpaceOnDisk(Path, True, FreeSpace, TotalSpace) then
    Result := FreeSpace >= MinSpace
  else
    RaiseException('Failed to check free space.');
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := True;

  if CurPageID = wpSelectDir then
  begin
    // the second parameter in this function call is the expected min. space in
    // units specified by the commented parameter above; in this example we are
    // checking if there's at least 1 MB of free space on drive of the selected
    // directory; we need to extract a drive portion of the selected directory,
    // because it's probable that the directory won't exist yet when we check
    if not IsEnoughFreeSpace(ExtractFileDrive(WizardDirValue), 1) then
    begin
      MsgBox('There is not enough space on drive of the selected directory. ' +
        'Setup will now exit.', mbCriticalError, MB_OK);
      // in this input parameter you can pass your own exit code which can have
      // some meaningful value indicating that the setup process exited because
      // of the not enough space reason
      ExitProcess(666);
    end;
  end;
end;

答案 1 :(得分:0)

也许我的回答看似偏离主题。 我或多或少有同样的问题。

如果你在文件部分有自己的检查功能,setup只能计算那些有“正常”检查标志的文件的(兆字节)字节数。

避免这种情况的一种方法是自己对字节进行计数,并将结果放在[setup]部分的ExtraDiskSpaceRequired指令中

相关问题