Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Direktes lesen auf dem physischen Datenträger

  Alt 11. Jun 2018, 17:14
Hmmm.... das erste war noch einfach für mich zu beantworten da es Winapi war.
Hier mische ich Delphi mit Winapi, keine Ahnung in wie fern das kompatibel zu Lazarus ist.
Folgender Code ermittelt alle Laufwerke mit Ihren Namen, da findest Du schon die passende Stelle wo Du Dich reinklinken kannst, wenn nicht, frage
Delphi-Quellcode:
function GetVolumeLabel(DriveChar: Char): string;
var
  NotUsed: DWORD;
  VolumeFlags: DWORD;
  VolumeInfo: array[0..MAX_PATH] of Char;
  VolumeSerialNumber: DWORD;
  Buf: array [0..MAX_PATH] of Char;
begin
    GetVolumeInformation(PChar(DriveChar + ':\'),
    Buf, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,
    VolumeFlags, nil, 0);
 
    SetString(Result, Buf, StrLen(Buf)); { Set return result }
    Result:=AnsiUpperCase(Result)
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  Drive : Char;
  DriveLetter : String;
begin
  For Drive := 'ATo 'ZDo
    begin
    DriveLetter := Drive + ':\';
    If GetDriveType(PChar(DriveLetter)) = DRIVE_FIXED Then
      begin
      Memo1.Lines.Add(Drive + ': ' + GetVolumeLabel(Drive));
    end;
  end;
end;
oder enumeriere die Laufwerke auf diese Winapi/Delphi Weise wie hier beschrieben.
Zitat:
The simplest way is actually to use GetDiskFreeSpaceEx from the sysutils.pas file.
There are 2 parts to this example. the 1st is the important part using GetDiskFreeSpaceEX.
Delphi-Quellcode:
function DriveSpace(DriveLetter : String; var FreeSpace, UsedSpace, TotalSpace : int64) : Boolean;
begin
  Result := SysUtils.GetDiskFreeSpaceEx(Pchar(DriveLetter), UsedSpace, TotalSpace, @FreeSpace);

  if UsedSpace > 0 then
    UsedSpace := TotalSpace - FreeSpace;

  if not Result then
  begin
    UsedSpace := 0;
    TotalSpace := 0;
    FreeSpace := 0;
  end;
end;
If you are going to request drives that you already know the drive letter for such as C: then that is all you need.
Usage would be something like:
Delphi-Quellcode:
var
  FS,
  US,
  TS : Int64
begin
  DriveSpace('C:', FS, US, TS);
  //Do something with the 3 variables.
end;
Having said that if you want to find the drives as well you could use something like this:
Delphi-Quellcode:
procedure ListDrivesOfType(DriveType : Integer; var Drives : TStringList);
var
  DriveMap,
  dMask : DWORD;
  dRoot : String;
  I : Integer;
begin
  dRoot := 'A:\'; //' // work around highlighting
  DriveMap := GetLogicalDrives;
  dMask := 1;

  for I := 0 to 32 do
  begin
    if (dMask and DriveMap) <> 0 then
      if GetDriveType(PChar(dRoot)) = DriveType then
      begin
        Drives.Add(dRoot[1] + ':');
      end;

    dMask := dMask shl 1;
    Inc(dRoot[1]);
  end;
end;
Note the DriveType integer, should be one of the following:
Delphi-Quellcode:
DRIVE_UNKNOWN = 0;
DRIVE_NO_ROOT_DIR = 1;
DRIVE_REMOVABLE = 2;
DRIVE_FIXED = 3;
DRIVE_REMOTE = 4;
DRIVE_CDROM = 5;
DRIVE_RAMDISK = 6;
(I have taken these straight out of windows.pas)
Now finally to answer your question (and this is very rough) the following would add information into a memo (called memo1) for all FIXED HARD DRIVES:
Delphi-Quellcode:
Procedure TAform.SomeNameICantThinkOfNow;
const
  BytesPerMB = 1048576;
var
  MyDrives : TStringlist;
  I : Integer;
  FreeSpace,
  UsedSpace,
  TotalSpace : int64;
begin
  MyDrives := TStringlist.Create;
  ListDrivesOfType(DRIVE_FIXED, MyDrives);

  Memo1.Lines.Clear;

  for I := 0 to MyDrives.Count - 1 do
  begin
    FreeSpace := 0;
    UsedSpace := 0;
    TotalSpace := 0;

    if DriveSpace(MyDrives.Strings[I], FreeSpace, UsedSpace, TotalSpace) then
    begin
      FreeSpace := FreeSpace div BytesPerMB;
      UsedSpace := UsedSpace div BytesPerMB;
      TotalSpace := TotalSpace div BytesPerMB;

      Memo1.Lines.Add('Drive: ' + MyDrives.Strings[I] + ' = Free Space :' + IntToStr(FreeSpace) +
                      ' Used Space: ' + IntToStr(UsedSpace) + ' Total Space: ' + IntToStr(TotalSpace));
    end;
  end;
end;
I did say it would be nasty! I have just run this up in the IDE and it works, I have done as MB but really you should convert to Double and choose your formatting if doing as MB to be more precise as the example I have create above will of course just round up.
Hope this is of some small assistance.
Gruß vom KodeZwerg

Geändert von KodeZwerg (11. Jun 2018 um 17:22 Uhr)
  Mit Zitat antworten Zitat