现在的位置: 首页 > 综合 > 正文

C++获取磁盘的信息

2013年12月08日 ⁄ 综合 ⁄ 共 3642字 ⁄ 字号 评论关闭

主要参考了两篇博客

http://www.cnblogs.com/qq78292959/archive/2012/06/12/2546914.html

http://blog.csdn.net/abidepan/article/details/7877329

 

众所周知,在微软的操作系统下编写应用程序,最主要的还是通过windows所提供的api函数来实现各种操作的,这些函数通常是可以直接使用的,只要包含windows.h这个头文件,
下载源文件

今天我们主要介绍的是几个常用的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 <windows.h>
class DistDrivers
{
public:
	virtual DWORD GetLogicalDrives(void)=0;
	virtual void GetLogicalDriveStrings()=0;
	virtual void GetDriveType(LPCTSTR lpRootPathName)=0;
	virtual void GetDiskFreeSpaceEx(LPCTSTR lpDirectoryName)=0;
public:
	int m_LogicalNum;//系统中逻辑驱动器的个数
	std::vector<wchar_t*> m_LogicalNames;//系统中所有的驱动器字符串的信息
	int m_DriveType;//驱动器类型
	float m_i64TotalBytes;//磁盘总容量
	float m_i64FreeBytesToCaller;//磁盘剩余容量
	float m_i64FreeBytes;
	LPTSTR sDrivePath;
};
#include <string.h>
#include <wchar.h>
class MyDistDrivers:public DistDrivers
{
public:
	DWORD GetLogicalDrives(void)
	{
		int DiskCount=0;
		DWORD DiskInfo=::GetLogicalDrives();
		while(DiskInfo)
		{
			if(DiskInfo&1)
			{
				++DiskCount;
			}
			DiskInfo=DiskInfo>>1;
		}
		m_LogicalNum=DiskCount;
		return m_LogicalNum;
	}
	void GetLogicalDriveStrings()
	{
		TCHAR buf[100];
		DWORD len=::GetLogicalDriveStrings(sizeof(buf)/sizeof(WCHAR),buf);
		for(wchar_t* s=buf;*s;s+=wcslen(s)+1)
		{
			sDrivePath=s;//单个盘符
			sDrivePath=(WCHAR*)malloc(sizeof(WCHAR)*(wcslen(s)+1));
			wcscpy(sDrivePath,s);
			m_LogicalNames.push_back(sDrivePath);
		}
	}
	void GetDriveType(LPCTSTR lpRootPathName)
	{
		//GetDriveType函数,可以获得驱动器的类型,参数为驱动器的根目录
		UINT DType=::GetDriveType(lpRootPathName);
		if(DType==DRIVE_FIXED)
		{
			cout<<"硬盘"<<endl;
		}
		else if(DType==DRIVE_CDROM)
		{
			cout<<"光驱"<<endl;
		}
		else if(DType==DRIVE_REMOVABLE)
		{
			cout<<"可移动的硬盘"<<endl;
		}
		else if(DType==DRIVE_REMOTE)
		{
			cout<<"网络磁盘"<<endl;
		}
		else if(DType==DRIVE_RAMDISK)
		{
			cout<<"虚拟RAM磁盘"<<endl;
		}
		else if(DType==DRIVE_UNKNOWN)
		{
			cout<"未知设备";
		}
	}
	void GetDiskFreeSpaceEx(LPCTSTR lpDirectoryName)
	{
		BOOL fResult=::GetDiskFreeSpaceEx(lpDirectoryName,
			(PULARGE_INTEGER)&m_i64FreeBytesToCaller,
			(PULARGE_INTEGER)&m_i64TotalBytes,
			(PULARGE_INTEGER)&m_i64FreeBytes);
		if(fResult)
		{
			m_i64FreeBytesToCaller=(float)(((PULARGE_INTEGER)&m_i64FreeBytesToCaller)->HighPart<<2)+(float)((PULARGE_INTEGER)&m_i64FreeBytesToCaller)->LowPart/(1024*1024*1024);
			m_i64TotalBytes=(float)(((PULARGE_INTEGER)&m_i64TotalBytes)->HighPart<<2)+(float)((PULARGE_INTEGER)&m_i64TotalBytes)->LowPart/(1024*1024*1024);
			cout<<"totalspace:"<<m_i64TotalBytes<<"GB"<<endl;//磁盘总容量
			cout<<"freespace:"<<m_i64FreeBytesToCaller<<"GB";//磁盘剩余容量
		}
		else
			cout<<"设备为准备好";
		cout<<endl;
	}
};
class CoreFactory
{
public:
	virtual DistDrivers* CreateInstance()=0;
};
class FactoryA:public CoreFactory
{
public:
	DistDrivers* CreateInstance()
	{
		return new MyDistDrivers();
	}
};
int main()
{
	
	FactoryA* pa=new FactoryA();
	DistDrivers* da=pa->CreateInstance();
	da->GetLogicalDriveStringsW();
	std::vector<wchar_t*>::iterator iter=da->m_LogicalNames.begin();
	for(;iter!=da->m_LogicalNames.end();iter++)
	{
		wcout<<*iter<<" ";
		//da->GetDriveTypeW(*iter);
		da->GetDiskFreeSpaceExW(*iter);
	}
	return 0;
}

 

 

抱歉!评论已关闭.