C++获取磁盘的信息

主要参考了两篇博客

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;
}


 

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,要获取磁盘卷标名称,可以使用以下步骤: 1. 首先,需要包含Windows.h头文件,这个头文件中定义了一些用于Windows系统编程的函数和数据结构。 2. 使用GetVolumeInformation函数来获取磁盘卷标名称。该函数的原型如下: BOOL GetVolumeInformationA( LPCSTR lpRootPathName, LPSTR lpVolumeNameBuffer, DWORD nVolumeNameSize, LPDWORD lpVolumeSerialNumber, LPDWORD lpMaximumComponentLength, LPDWORD lpFileSystemFlags, LPSTR lpFileSystemNameBuffer, DWORD nFileSystemNameSize ); 其中,lpRootPathName是一个指向包含卷标的根路径的字符串,比如"C:\";lpVolumeNameBuffer是用来存放卷标名称的缓冲区;nVolumeNameSize是缓冲区大小;其他的参数可以传入NULL。 3. 创建一个字符数组,用来存储卷标名称。 4. 调用GetVolumeInformation函数,并传入磁盘根路径和字符数组作为参数。 5. 如果函数返回TRUE,表示获取卷标名称成功,可以通过打印字符数组的值来查看卷标名称。 以下是一个示例代码: ```c #include <Windows.h> #include <stdio.h> int main() { char volumeName[MAX_PATH]; char rootPath[] = "C:\\"; if (GetVolumeInformationA(rootPath, volumeName, sizeof(volumeName), NULL, NULL, NULL, NULL, 0)) { printf("磁盘卷标名称: %s\n", volumeName); } else { printf("无法获取磁盘卷标名称\n"); } return 0; } ``` 以上是用C语言获取磁盘卷标名称的方法。注意,在Windows系统上,不是所有的磁盘都会有卷标名称,所以有可能获取到的名称为空。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值