As we all know, writing applications in Microsoft's Operating SystemProgramThe most important thing is to implement various operations through the API functions provided by windows. These functions can be directly used, as long as they contain the windows. h header file and download the source file.
Today we will mainly introduce several common API functions, through which we can obtain information about the user's disk.
The main function prototype is described as follows:
1.Obtain the number of logical drives in the system
TheGetlogicaldrivesFunction retrieves a bitmask representing the currently available disk drives.
DWORD Getlogicaldrives (void );
2.Get all drive string Information
TheGetlogicaldrivestringsFunction fills a buffer with strings that specify valid drives in the system.
DWORD Getlogicaldrivestrings (
DWORD Nbufferlength,
Lptstr Lpbuffer
);
3.Get Drive Type
TheGetdrivetypeFunction determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
Uint Getdrivetype (
Lpctstr Lprootpathname
);
4.Obtains the space status of the drive disk. The function returns bool data.
TheGetdiskfreespaceexFunction retrieves information about the amount of space available on a disk volume: the total amount 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 a complete example program.Code
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, please note the source and author.
# Include <iostream>
# Include <windows. h>
Using namespace STD;
Int main ()
{
Int diskcount = 0;
DWORD diskinfo = getlogicaldrives ();
// Use the getlogicaldrives () function to obtain the number of logical drives in the system. The function returns a 32-bit unsigned integer data.
While (diskinfo) // you can view whether each bit of data is 1 in a loop. If it is 1, the disk is true. If it is 0, the disk does not exist.
{
If (diskinfo & 1) // logic and operation of bit operations to determine whether it is 1
{
++ Diskcount;
}
Diskinfo = diskinfo> 1; // The right shift operation of bitwise Operations ensures that the position checked at each loop is shifted to the right.
// Diskinfo = diskinfo/2;
}
Cout <"Logical Disk quantity:" <diskcount <Endl;
//-------------------------------------------------------------------
Int dslength = getlogicaldrivestrings (0, null );
// Use the getlogicaldrivestrings () function to obtain the length of all drive string information.
Char * dstr = new char [dslength]; // create a C-style string array in the heap with the obtained Length
Getlogicaldrivestrings (dslength, (lptstr) dstr );
// Use getlogicaldrivestrings to copy the string information to the heap array, where information about all the drives is saved.
Int dtype;
Int Si = 0;
Bool fresult;
Unsigned _ int64 i64freebytestocaller;
Unsigned _ int64 i64totalbytes;
Unsigned _ int64 i64freebytes;
For (INT I = 0; I <dslength/4; ++ I)
// To display the status of each drive, it is implemented through cyclic output. Because the data stored in dstr is a: \ nullb: \ nullc: \ null, so dslength/4 can obtain the specific large loop range.
{
Char dir [3] = {dstr [Si], ':', '\'};
Cout <dir;
Dtype = getdrivetype (dstr + I * 4 );
// Getdrivetype function, which can be used to obtain the drive type. The parameter is the root directory of the drive.
If (dtype = drive_fixed)
{
Cout <"Hard Disk ";
}
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 <"virtual RAM disk ";
}
Else if (dtype = drive_unknown)
{
Cout <"unknown device ";
}
Fresult = getdiskfreespaceex (
Dir,
(Pularge_integer) & i64freebytestocaller,
(Pularge_integer) & i64totalbytes,
(Pularge_integer) & i64freebytes );
// Getdiskfreespaceex function, which can obtain the space status of the drive disk. The function returns bool data.
If (fresult) // determines whether the drive is in the working state through the returned bool data
{
Cout <"totalspace:" <(float) i64totalbytes/1024/1024 <"MB"; // total disk capacity
Cout <"freespace:" <(float) i64freebytestocaller/1024/1024 <"MB"; // remaining disk space
}
Else
{
Cout <"the device is not ready ";
}
Cout <Endl;
Si + = 4;
}