How to Check whether required space available in the hard disk to install application using inno setup installer

后端 未结 2 604
感动是毒
感动是毒 2021-01-07 07:07

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

相关标签:
2条回答
  • 2021-01-07 07:48

    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

    0 讨论(0)
  • 2021-01-07 07:54

    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;
    
    0 讨论(0)
提交回复
热议问题