C++获取系统盘符信息

C++获取系统盘符信息


完整代码

#include 
#include 
#include 
#include 

void getDiskInfo()
{
	char rootPath[10] = { 0 };
	int isExist = 0;
	int diskNum = 0;
	int countTotal = 0;
	int type = 0;

	printf("磁盘为:");
	for (char a = 'A'; a <= 'Z'; a++) //获取所有盘符 
	{
		sprintf_s(rootPath, "%c:\\", a);
		//isExist值的类别 0:exist 用来检查目录是否存在,2:write 写权限,4:read 读权限,3:write-read 读写权限
		isExist = _access(rootPath, 0);
		
		if (isExist == 0)
		{
			countTotal++;
			printf("%s\t", rootPath);
		}
	}

	printf("\n硬盘个数为:");
	for (wchar_t a = 'A'; a <= 'Z'; a++) //获取本地硬盘盘符数
	{
		wchar_t diskName[10] = { a, L': ' };
		//type的类别 1:可移动磁盘,2:软盘,3:本地硬盘,4:网络磁盘,5:CD-ROM,6:RAM磁盘
		type = GetDriveTypeW(diskName); 
		if (type == 3) {
			diskNum++;
		}
	}
	printf("%d\n", diskNum);

}
void main()
{
	getDiskInfo();
}

运行结果

磁盘为:C:\     D:\
硬盘个数为:2

你可能感兴趣的:(C/C++,c++,c语言,vc,安全,visual,studio,code)