逻辑驱动器个数和盘符获取GetLogicalDrives和GetLogicalDriverStrings

获取逻辑驱动器有两个函数GetLogicalDrives和GetLogicalDriverStrings

MSDN:GetLogicalDrives函数介绍

MSDN :GetLogicalDriverStrings函数介绍

一、盘符和标卷的获取 GetLogicalDriverStrings


// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include   <windows.h> 
#include   <stdio.h> 

#define BUFSIZE 1024

int main(int   argc, char   **argv)
{
 
	TCHAR VolumeName[256];
	
 
	TCHAR szLogicDriveStrings[BUFSIZE];
	PCHAR szDrive;

	ZeroMemory(szLogicDriveStrings, BUFSIZE);

	GetLogicalDriveStrings(BUFSIZE - 1, szLogicDriveStrings);
	szDrive = (PCHAR)szLogicDriveStrings;
	 
	/*方法二,采用do循环才能GetDriveType(szDrive);
	
do
	{
		UINT uDriverType;
		printf("\n%s\n", szDrive);
		uDriverType = GetDriveType(szDrive);


		switch (uDriverType)
		{
		case DRIVE_UNKNOWN:
			printf("The driver type cannot be determined!");
			break;
		case DRIVE_NO_ROOT_DIR:
			printf("The root path is invalid,for example,no volume is mounted at the path");
			break;
		case DRIVE_REMOVABLE:
			printf("The drive is a type that has removable media,for example:a floppy drive or removable hard disk");
			break;
		case DRIVE_FIXED:
			printf("The drive is a type that cannot be removed, for example,a fixed hard drive");
			break;
		case DRIVE_REMOTE:
			printf("This drive is a remote(network) drive");
			break;
		case DRIVE_CDROM:
			printf("This drive is a CD-ROM drive.");
			break;
		case DRIVE_RAMDISK:
			printf("This drive is a RAM disk");
			break;
		default:
			break;
		}
		szDrive += (lstrlen(szDrive) + 1);


	} while (*szDrive != '\x00');

	*/
	while(*szDrive)
	{
		GetVolumeInformation(szDrive, VolumeName, 255, NULL, NULL, NULL, NULL, NULL);
		printf("%s的卷标:%s\n ", szDrive, VolumeName);
		szDrive+= (_tcslen(szDrive) + 1);
	}
	//system("PAUSE");
	return   0;
}


二,个数获取GetLogicalDrives


// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include   <windows.h> 
#include   <stdio.h> 

#define BUFSIZE 1024
int main()
{
	DWORD temp;
	char disk_path[5] = { 0 };
	int count = 0;
	int diskmount = 0;
	temp = GetLogicalDrives();
	while (temp != 0)
	{
		if ((temp & 1) != 0)
		//if ((temp & 0x1) == 1)
		{
		
			sprintf_s(disk_path, "%c:", 'A' + count);
			printf("%s \n", disk_path);
			diskmount++;
		
		}
		temp >>= 1;
		count++;
	}
	 printf("驱动器个数:%d\n", diskmount);
 
	return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值