As we all know, in the Microsoft operating system to write applications, the most important thing is through the windows provided by the API functions to achieve a variety of operations, these functions are usually directly available, as long as the windows.h this header file is included. Today we mainly introduce a few common API functions, through which we can get information about the user's disk. Sample program: Please click on the attachment download. The main function prototypes are described below: 1. Get the number of logical drives in the system The getlogicaldrives function retrieves a bitmask representing the currently available disk drives. DWORD getlogicaldrives (void);
2. Get all drive string information The GetLogicalDriveStrings function fills a buffer with strings this specify valid drives in the system. DWORD GetLogicalDriveStrings ( DWORD Nbufferlength, LPTSTR lpbuffer ); 3. Get the Drive type The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive. UINT GetDriveType ( LPCTSTR lpRootPathName ); 4. Gets the space state of the drive disk, and the function returns a bool type of data The GetDiskFreeSpaceEx function retrieves information about the amount of space available on a disk volume:the total Amou NT of space, the total amount of free space, and the total amount of free space available to the user associated with the Calling thread. BOOL GetDiskFreeSpaceEx ( LPCTSTR Lpdirectoryname, Pularge_integer lpfreebytesavailable, Pularge_integer Lptotalnumberofbytes, Pularge_integer lptotalnumberoffreebytes ); The following is the complete sample program code: 2 back top #include<Iostream> #include<Windows.h> Usingnamespace Std;
IntMain () { IntDiskcount = 0; DWORDDiskInfo =GetLogicalDrives (); UseGetLogicalDrives() function can get the number of logical drives in the system, and the function returns a32-bit unsigned integer data. WhileDiskInfo)//To see if each bit of data is1, if the1 The disk is True, if the0 The disk does not exist. { if (diskinfo&1)//Through the logic and operation of the bitwise operation, determine whether the1 { ++Diskcount; } DiskInfo =DiskInfo >> 1;//The right shift of the bitwise operation ensures that each loop is checked to the right one bit at a time. //DiskInfo = DISKINFO/2; } cout<< "Number of logical disks: "<<diskcount<<Endl //-------------------------------------------------------------------
IntDslength =GetLogicalDriveStrings (0,null); PassGetLogicalDriveStrings() function to obtain the length of all drive string information. char*Dstr = new char[dslength];//creates a heap area with the obtained lengthC-style string array GetLogicalDriveStrings (Dslength, (LPTSTR)DSTR); PassGetLogicalDriveStrings copying string information into a heap array, which holds information for all drives.
IntDType; Intsi=0; BOOLFresult; unsigned _int64 i64freebytestocaller; unsigned _int64 i64totalbytes; unsigned _int64 i64freebytes;
ForIntI=0;i<dslength/4;++i) In order to show the status of each drive, it is implemented through the loop output becauseThe data stored inside the DSTR isA:\nullb:\nullc:\null, such a message, soDSLENGTH/4 can obtain specific cycle range { Char dir[3]={dstr[Si], ': ', ' \ \ '}; cout<<dir; DType =GetDriveType (DSTR+I*4); //GetDriveType function, you can get the drive type, the parameter is the root directory of the drive IfDType = = drive_fixed) { cout<< "hdd"; } else if (DType = = Drive_cdrom) { cout<< "Optical Drive"; } else if (DType = = drive_removable) { cout<< "Removable disk"; } else if (DType = = drive_remote) { cout<< "Network disk"; } else if (DType = = Drive_ramdisk) { cout<< "virtualRAM Disk"; } else if (DType = = Drive_unknown) { cout<< "Unknown Device"; }
Fresult =GetDiskFreeSpaceEx ( Dir (Pularge_integer) &i64freebytestocaller, (Pularge_integer) &i64totalbytes, (Pularge_integer) &i64freebytes); //GetDiskFreeSpaceEx function to get the space state of the drive disk, the function returns aBOOL Type data IfFresult)//by returning theBOOL data determines if the drive is in a working state { cout<< "Totalspace: "<< (float) i64totalbytes/1024/1024<<" MB ";//Disk Total capacity cout<< "FreeSpace: "<< (float) i64freebytestocaller/1024/1024<<" MB ";//Disk space remaining } else { cout<< " Device not ready } cout<<ENDL; si+=4; } system ("pause"); /span> The 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. |