How to Check whether required space available in t

2019-02-20 18:19发布

问题:

I have built an installer to install application using inno setup.But i want to display an error message showing that there is not enough space in the drive or path where i am going to install application if there is no space available. By default i am getting inno built in ability to show message when there is no space available in the hard disk or selected path. but it shows YES and NO button to continue or cancel . Here i want to show error message with a OK button and when the user clicks ok button it should stop installation. Please help me on this issue. i could not find any ways to do so. i have called both GetSpaceOnDisk64() and GetSpaceOnDisk() which are not working and throwing exception

i am getting the error . please have a look at the attached image

回答1:

To determine a free space on a drive of a specific folder (in your case the selected directory), you can call the GetSpaceOnDisk or GetSpaceOnDisk64 function. The difference between them is that the first one is able to return space info in bytes as well as in megabytes. The latter returns this info just in bytes. For the following example I chose the first mentioned function, so you can decide in which units you want to operate by modifying a single boolean parameter:

[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;


回答2:

Maybe my answer looks like off-topic. I had more or less the same problem.

If you have in the files section a check function made by yourself, setup can only count the number of (Mega)bytes of those files which have "normal" check flags.

A way to avoid this is count-up the bytes by yourself and put the result in the ExtraDiskSpaceRequired directive in the [setup] section



标签: inno-setup