设为首页 加入收藏

TOP

C++通过WIN32API获取逻辑磁盘详细信息
2014-11-23 21:26:57 】 浏览:470
Tags:通过 WIN32API 获取 逻辑 磁盘 详细 信息

  今天我们主要介绍的是几个常用的api函数,通过它我们可以获取用户磁盘的相关信息。


  示例程序:请点击附件下载。  


  其主要函数原型说明如下:


  1.获取系统中逻辑驱动器的数量


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


  DWORD GetLogicalDrives(void);
  


  2.获取所有驱动器字符串信息


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


  DWORD GetLogicalDriveStrings(
  DWORD nBufferLength,
  LPTSTR lPBuffer
  );


  3.获取驱动器类型


  The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.


  UINT GetDriveType(
  LPCTSTR lpRootPathName
  );


  4. 获取驱动器磁盘的空间状态,函数返回的是个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
  );


  以下是完整的示例程序代码:


  #include
  #include
  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<<"逻辑磁盘数量:"<   //-------------------------------------------------------------------


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇使用虚拟变量和格式说明符进行X64.. 下一篇运行中程序删除自己的方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目