VC obtain disk space

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/mao0514/article/details/89151764

As we all know, writing in the Microsoft operating system applications, the most important thing to realize various operations through windows api functions provided by these functions can usually be directly used, as long as it contains the header files windows.h.

  Today we introduce are several common api function, through which we can get information about the user's disk.

 

 

 Its main function prototype follows:

  1. Get the number of logical drives the system

The GetLogicalDrives function retrieves a bitmask representing the currently available disk drives.

DWORD GetLogicalDrives(void);
 

  2. Get all drives string information

The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.

DWORD GetLogicalDriveStrings(
  DWORD nBufferLength,
  LPTSTR lpBuffer
);

  3. Get 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. Get the drive disk space status, the function returns the data type is BOOL

The GetDiskFreeSpaceEx function 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 sample program code:


 int cheakDiskFreeSpace()
{
	int retu=0;
	char * strRootPath="E:\\";//*///带根目录标记的磁盘符号  
    DWORD dwSectorsPerCluster=0;//每簇中扇区数  
    DWORD dwBytesPerSector=0;//每扇区中字节数  
    DWORD dwFreeClusters=0;//剩余簇数  
    DWORD dwTotalClusters=0;//总簇数  
    if (GetDiskFreeSpace(char2wchar(strRootPath),&dwSectorsPerCluster,&dwBytesPerSector,  
        &dwFreeClusters,&dwTotalClusters))  
    {  
		diskStatus[0] =1;//检测到E
        double dd=dwSectorsPerCluster*dwBytesPerSector/(1024.*1024.);  
        dd=dd/1024.;  
        double m_dFree=dwFreeClusters*dd;//该磁盘剩余容量总大小  
        if (m_dFree < 1)  
        {  
           diskStatus[0] |= 0x0100;//空间不足
		   retu++;
        }  
		else
		{
		    diskStatus[0] |= 0x0000;//空间足
		}
		
    }
	else
	{
		retu ++;
	    diskStatus[0] =0;//no检测到E
	}

	 return retu;
}

#include <iostream>
#include <windows.h>
using namespace std;  
 
int main()
{
     int DiskCount = 0;
     DWORD DiskInfo = GetLogicalDrives();
     //利用GetLogicalDrives()函数可以获取系统中逻辑驱动器的数量,函数返回的是一个32位无符号整型数据。
     while(DiskInfo)//通过循环操作查看每一位数据是否为1,如果为1则磁盘为真,如果为0则磁盘不存在。
     {
         if(DiskInfo&1)//通过位运算的逻辑与操作,判断是否为1
         {
              ++DiskCount;
         }
         DiskInfo = DiskInfo >> 1;//通过位运算的右移操作保证每循环一次所检查的位置向右移动一位。
         //DiskInfo = DiskInfo/2;
     }
     cout<<"逻辑磁盘数量:"<<DiskCount<<endl;
//-------------------------------------------------------------------
 
      int DSLength = GetLogicalDriveStrings(0,NULL);
     //通过GetLogicalDriveStrings()函数获取所有驱动器字符串信息长度。
     char* DStr = new char[DSLength];//用获取的长度在堆区创建一个c风格的字符串数组
     GetLogicalDriveStrings(DSLength,(LPTSTR)DStr);
     //通过GetLogicalDriveStrings将字符串信息复制到堆区数组中,其中保存了所有驱动器的信息。
 
     int DType;
     int si=0;
     BOOL fResult;
    unsigned _int64 i64FreeBytesToCaller;
    unsigned _int64 i64TotalBytes;
    unsigned _int64 i64FreeBytes;
 
      for(int i=0;i<DSLength/4;++i)
     //为了显示每个驱动器的状态,则通过循环输出实现,由于DStr内部保存的数据是A:\NULLB:\NULLC:\NULL,这样的信息,所以DSLength/4可以获得具体大循环范围
     {
         char dir[3]={DStr[si],':','\\'};
         cout<<dir;
         DType = GetDriveType(DStr+i*4);
         //GetDriveType函数,可以获取驱动器类型,参数为驱动器的根目录
         if(DType == DRIVE_FIXED)
         {
              cout<<"硬盘";
         }
         else if(DType == DRIVE_CDROM)
         {
              cout<<"光驱";
         }
         else if(DType == DRIVE_REMOVABLE)
         {
              cout<<"可移动式磁盘";
         }
         else if(DType == DRIVE_REMOTE)
         {
              cout<<"网络磁盘";
         }
         else if(DType == DRIVE_RAMDISK)
         {
              cout<<"虚拟RAM磁盘";
         }
         else if (DType == DRIVE_UNKNOWN)
         {
              cout<<"未知设备";
         }
 
         fResult = GetDiskFreeSpaceEx (
              dir,
              (PULARGE_INTEGER)&i64FreeBytesToCaller,
              (PULARGE_INTEGER)&i64TotalBytes,
              (PULARGE_INTEGER)&i64FreeBytes);
         //GetDiskFreeSpaceEx函数,可以获取驱动器磁盘的空间状态,函数返回的是个BOOL类型数据
         if(fResult)//通过返回的BOOL数据判断驱动器是否在工作状态
         {
              cout<<" totalspace:"<<(float)i64TotalBytes/1024/1024<<" MB";//磁盘总容量
              cout<<" freespace:"<<(float)i64FreeBytesToCaller/1024/1024<<" MB";//磁盘剩余空间
         }
         else
         {
              cout<<" 设备未准备好";
         }
         cout<<endl;
         si+=4;
     }
 
     system("pause");
}
 
  WIN API函数作为windows操作系统环境下编程的接口,在其它语言,例如VB VB.NET C# DELPHI中使用的时候操作的方法也是差不多的,具体可以参考相关语言的书籍。

 

Guess you like

Origin blog.csdn.net/mao0514/article/details/89151764