1#include <iostream>2#include <windows.h>3 using namespacestd; 4 5 intMain ()6 {7 intDiskcount =0;8DWORD DiskInfo =getlogicaldrives ();9 //The getlogicaldrives () function is used to obtain the number of logical drives in the system, and the function returns a 32-bit unsigned integer data. Ten while(DiskInfo)//The loop operation is to see if each bit of data is 1, if 1 the disk is true and if 0 the disk does not exist. One { A if(diskinfo&1)//by the logic and operation of the bitwise operation, it is judged whether it is 1 - { -++Diskcount; the } -DiskInfo = DiskInfo >>1;//The right-shift operation of the bitwise operation guarantees that each loop is checked to the right by one position at a time. - //diskinfo = DISKINFO/2; - } +cout<<"Number of logical disks:"<<DiskCount<<Endl; - //------------------------------------------------------------------- + A intDslength = GetLogicalDriveStrings (0, NULL); at //gets the length of all drive string information through the GetLogicalDriveStrings () function. - Char* Dstr =New Char[Dslength];//creates a C-style string array in the heap area with the obtained length - getlogicaldrivestrings (Dslength, (LPTSTR) dstr); - //The string information is copied to the heap array through getlogicaldrivestrings, where information about all the drives is saved. - - intDType; in intSi=0; - BOOL Fresult; to unsigned _int64 i64freebytestocaller; + unsigned _int64 i64totalbytes; - unsigned _int64 i64freebytes; the * for(intI=0; i<dslength/4;++i) $ //in order to show the status of each drive, it is implemented through the loop output, because the data stored inside the DSTR is a:\nullb:\nullc:\null, so DSLENGTH/4 can obtain a specific cycle rangePanax Notoginseng { - Chardir[3]={dstr[si],':','\\'}; thecout<<dir; +DType = GetDriveType (dstr+i*4); A //getdrivetype function, you can get the drive type, the parameter is the root directory of the drive the if(DType = =drive_fixed) + { -cout<<"HDD"; $ } $ Else if(DType = =drive_cdrom) - { -cout<<"Optical Drive"; the } - Else if(DType = =drive_removable)Wuyi { thecout<<"Removable disk"; - } Wu Else if(DType = =drive_remote) - { Aboutcout<<"Network Disk"; $ } - Else if(DType = =Drive_ramdisk) - { -cout<<"Virtual RAM Disk"; A } + Else if(DType = =Drive_unknown) the { -cout<<"Unknown Device"; $ } the theFresult =GetDiskFreeSpaceEx ( the dir, the(Pularge_integer) &I64freebytestocaller, -(Pularge_integer) &I64totalbytes, in(Pularge_integer) &i64freebytes); the //The GetDiskFreeSpaceEx function can get the space state of the drive disk, and the function returns a bool type of data the if(Fresult)//determines whether the drive is in a working state by returning the BOOL data About { thecout<<"Totalspace:"<< (float) i64totalbytes/1024x768/1024x768<<"MB";//Total disk Capacity thecout<<"FreeSpace:"<< (float) i64freebytestocaller/1024x768/1024x768<<"MB";//disk space remaining the } + Else - { thecout<<"The device is not ready.";Bayi } thecout<<Endl; thesi+=4; - } - theSystem"Pause"); the } theThe WIN API functions as a programming interface in the Windows operating system environment, in other languages, such as VB vb.net C # Delphi in the use of the method of operation is similar, specific reference to the relevant language books.
C + + obtains logical disk details through the WIN32 API