Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Number of disks? (https://www.delphipraxis.net/98241-number-disks.html)

Razor 23. Aug 2007 18:24


Number of disks?
 
Maybe with code is it possible to get number of disks(any kind SATA,PATA,IDE dosent matter).If its possible please post! :?

mkinzler 23. Aug 2007 18:25

Re: Number of disks?
 
WMI

Razor 23. Aug 2007 18:27

Re: Number of disks?
 
Not possible with WMI you can only get this


Value, errinfo, model, serial, diskbytes

mkinzler 23. Aug 2007 18:29

Re: Number of disks?
 
If you can get all drives, you can alos count them.

hathor 23. Aug 2007 19:03

Re: Number of disks?
 
Delphi-Quellcode:

Procedure TForm1.GetHDDcount (Sender: TObject);
var f, i: integer;
begin
  i := 0;
  repeat
    f := FileOpen('\\.\PHYSICALDRIVE' + IntToStr(i), fmOpenRead or fmShareDenyNone);
    if f = -1 then break;
//    Listbox1.Items.Add('PHYSICALDRIVE'+IntToStr(i));
    inc (i);
  until false;
HDDcount1.Caption := IntToStr(i)+' HDD';
end;
// or
function GetHDDCount: integer;
var f, i: integer;
begin
  i := 0;
  repeat
    f := FileOpen('\\.\PHYSICALDRIVE' + IntToStr(i), fmOpenRead or fmShareDenyNone);
    if f = -1 then break;
    inc (i);
  until false;
  result := i;
end;

himitsu 29. Okt 2007 17:09

Re: Number of disks?
 
ich wollt nur mal darauf hinweisen, das der Code von hathor nicht 100% funktionieren wird.
denn wenn eine "Disk" aus dem laufendem System entfernt wird (es reicht wenn ein USB-Stick o.Ä. entfernt wird), dann werden die Nummern der nachfolgenden Disks nicht geändert, wobei dann eine "Lücke" entsteht, an welcher sein Code aufhört zu zählen.

Code:
Bsp:
\\.\PhysicalDrive0   -   HDD 1
\\.\PhysicalDrive1   -   HDD 2
\\.\PhysicalDrive2   -   USB 1
\\.\PhysicalDrive3   -   USB 2


USB 1 entfernt:
\\.\PhysicalDrive0   -   HDD 1
\\.\PhysicalDrive1   -   HDD 2

\\.\PhysicalDrive3   -   USB 2


3 Laufwerke vorhanden, es würden aber nur 2 gezählt.

einfache Lösung:
Delphi-Quellcode:
function GetHDDCount: integer;
var f, i: integer;
begin
  Result := 0;
  for i := 0 to 255 do
  begin
    f := FileOpen('\\.\PHYSICALDRIVE' + IntToStr(i), fmOpenRead or fmShareDenyNone);
    if f = INVALID_HANDLE_VALUE then continue;
    FileClose(f);
    inc(Result);
  end;
end;
(statt 255 würde meistens auch schon 9 ausreichen, aber sicher is sicher :angel: )


PS: die geöffnete Laufwerke sollten besser auch wieder geschlossen werden !!!


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:36 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz