Solved

GetDiskFreeSpaceEx

Posted on 2000-04-01
17
349 Views
Last Modified: 2010-04-04
Is available any demo app that can show disk free space ?

 with theese function or other ?

 Esk
0
Comment
Question by:esk
  • 8
  • 6
  • 3
17 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 10 total points
ID: 2676831
Use the DiskFree function:

function DiskFree(Drive: Byte): Int64;

0 = current drive
1 = A:
2 = B:
3 = C:
etc...
0
 
LVL 17

Expert Comment

by:inthe
ID: 2677014
The functions DiskSize & DiskFree are not capable of handling drives larger than 2 gigabyte..

THIS IS FROM SOME FAQ:
FAQ2552D.txt   Checking available disk space on large drives.
Category   :Windows API
Platform    :All
Product    :All 32 bit  

Question:
How do I check for available diskspace on a drive larger than 2
gigabytes?


Answer:
You will need to call the Windows API function GetDiskFreeSpaceEx()
and convert the returned integers to doubles, since integers greater
than 2 gigabytes are not supported in Delphi.

Example:

function GetDiskFreeSpaceEx(lpDirectoryName: PAnsiChar;
  var lpFreeBytesAvailableToCaller : Integer;
  var lpTotalNumberOfBytes: Integer;
  var lpTotalNumberOfFreeBytes: Integer) : bool;
  stdcall;
  external kernel32
  name 'GetDiskFreeSpaceExA';


procedure GetDiskSizeAvail(TheDrive : PChar;
                           var TotalBytes : double;
                           var TotalFree : double);
var
  AvailToCall : integer;
  TheSize : integer;
  FreeAvail : integer;
begin
  GetDiskFreeSpaceEx(TheDrive,
                     AvailToCall,
                     TheSize,
                     FreeAvail);
{$IFOPT Q+}
 {$DEFINE TURNOVERFLOWON}
 {$Q-}
{$ENDIF}
  if TheSize >= 0 then
    TotalBytes := TheSize else
  if TheSize = -1 then begin
    TotalBytes := $7FFFFFFF;
    TotalBytes := TotalBytes * 2;
    TotalBytes := TotalBytes + 1;
  end else
  begin
    TotalBytes := $7FFFFFFF;
    TotalBytes := TotalBytes + abs($7FFFFFFF - TheSize);
  end;

  if AvailToCall >= 0 then
    TotalFree := AvailToCall else
  if AvailToCall = -1 then begin
    TotalFree := $7FFFFFFF;
    TotalFree := TotalFree * 2;
    TotalFree := TotalFree + 1;
  end else
  begin
    TotalFree := $7FFFFFFF;
    TotalFree := TotalFree + abs($7FFFFFFF - AvailToCall);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  TotalBytes : double;
  TotalFree : double;
begin
  GetDiskSizeAvail('C:\',
                   TotalBytes,
                   TotalFree);
  ShowMessage(FloatToStr(TotalBytes));
  ShowMessage(FloatToStr(TotalFree));
end;


Regards Barry
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2677026
>The functions DiskSize & DiskFree are not capable of handling drives larger than 2 gigabyte..

In Delphi 4 and 5 they work perfectly!
0
 
LVL 17

Expert Comment

by:inthe
ID: 2677060
mmm..i had to search to some searching cause i was sure i was right about the drive limits.and yes your correct for d4/5 , for other versions of delphi it returns longint so that is why the drive size limit of less than 2gig. ;-)
0
 

Author Comment

by:esk
ID: 2677063
Inthe, i get an error in this line

 GetDiskFreeSpaceEx(TheDrive,
                                           AvailToCall,
                                           TheSize,
                                           FreeAvail);
Not integer!

Esk
0
 

Author Comment

by:esk
ID: 2677068
Epsylon, this function does not return same size as Windows

function DiskFree(Drive: Byte): Int64;

Esk
0
 
LVL 17

Expert Comment

by:inthe
ID: 2677070
what version of delphi you using ?
0
 

Author Comment

by:esk
ID: 2677073
Here is a component that can shows free diskspace but it's .dcu file

http://www.besse.de/delphi/d40flib.htm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:esk
ID: 2677074
Delphi 5. professional
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2677077
Esk, DiskFree uses GetDiskFreeSpace internally. That means that GetDiskFreeSpace and GetDiskFreeSpaceEx will return the same values as DiskFree.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2677080
I have a 27 GB HDD here and DiskFree returns exactly the same as Windows tells me....
0
 

Author Comment

by:esk
ID: 2677083
I do this

Button1.Caption := IntToStr(DiskSize(3) Div 1024 Div 1024);
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2677089
DiskSize? I though you needed DiskFree..?

Anyway, both function work ok here.
0
 

Author Comment

by:esk
ID: 2677094
This works

Esk
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2677097
Alright, I can go to bed now   :o)
0
 

Author Comment

by:esk
ID: 2677124
i have one more.

how can i detect if no disk is in drive a:\

Esk
0
 

Author Comment

by:esk
ID: 2677561
it's okay, i have solve the problem. Found code.

Esk
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now