Windows中卷(Volume)操作,获取卷GUID、磁盘类型、磁盘大小等API

Windows中卷(Volume)操作

1、阅读msdn整理卷相关的知识点

https://docs.microsoft.com/zh-cn/windows/win32/fileio/volume-management
Windows中卷(Volume)操作,获取卷GUID、磁盘类型、磁盘大小等API_第1张图片

2、重点讲解几个常用的API及其示例

2.1、GetVolumeNameForVolumeMountPoint

用于获取卷的GUID路径,通过与该卷相关的挂载点(mount point)(如:盘符(drive letter),挂载文件夹等)。

API:

BOOL GetVolumeNameForVolumeMountPointW(
  [in]  LPCWSTR lpszVolumeMountPoint,
  [out] LPWSTR  lpszVolumeName,
  [in]  DWORD   cchBufferLength
);

示例代码:

#include 
#include 
#include 

#define BUFSiZE MAX_PATH

void main(void)
{
	BOOL bFlag;
	TCHAR buf[BUFSiZE];
	TCHAR Drive[] = TEXT("c:\\");
	TCHAR I;
	for (I = TEXT('c'); I < TEXT('z'); ++I){
		Drive[0] = I;
		bFlag = GetVolumeNameForVolumeMountPoint(
			Drive, buf, BUFSiZE);

		if (bFlag){
			/**
			result:
			The Id of drive "c:\" is "\\?\Volume{252dcdf3-1487-4c35-a7a6-bd5ad25feb35}\"
            The Id of drive "d:\" is "\\?\Volume{9713a0fb-9880-4a80-8a7d-2adbc29d846f}\"
            The Id of drive "e:\" is "\\?\Volume{8348e8d4-d652-11ec-8a07-50814024957f}\"
			*/
			_tprintf(TEXT("The Id of drive \"%s\" is \"%s\"\n"), Drive, buf);
		}
	}
	return;
}

2.2、GetDriveType(A/W)

用于确定指定的磁盘驱动器是移动的(removable)、固定的(fixed)、CD-ROM、RAMDISK还是网络设备。

确定一个设备是否是USB类型,可以调用SetupDiGetDeviceRegistryProperty并指定SPDRP_REMOVAL_POLICY属性。

API:

UINT GetDriveTypeW(
  [in, optional] LPCWSTR lpRootPathName
);

示例代码:

#include 
#include 
#include 

int main()
{
	UINT type = GetDriveType("E:\\test.m4a");
	switch (type)
	{
	case DRIVE_UNKNOWN:
		std::cout << "unknow" << std::endl;
		break;
	case DRIVE_NO_ROOT_DIR:
		std::cout << "no root dir" << std::endl;
		break;
	case DRIVE_REMOVABLE:
		std::cout << "removable" << std::endl;
		break;
	case DRIVE_FIXED:
		std::cout << "fixed" << std::endl;
		break;
	case DRIVE_REMOTE:
		std::cout << "remote" << std::endl;
		break;
	case DRIVE_CDROM:
		std::cout << "cdrom" << std::endl;
		break;
	case DRIVE_RAMDISK:
		std::cout << "ramdisk" << std::endl;
		break;
	default:
		break;
	}
	return 0;
}

2.3、GetDiskFreeSpaceEx(A/W)

用于获取一个磁盘空间数量信息,如磁盘总大小、剩余大小以及用户可用的剩余大小。

注意:单位是bytes

API:

// 数据结构
#if defined(MIDL_PASS)
typedef struct _ULARGE_INTEGER {
#else // MIDL_PASS
typedef union _ULARGE_INTEGER {
    struct {
        DWORD LowPart;
        DWORD HighPart;
    } DUMMYSTRUCTNAME;
    struct {
        DWORD LowPart;
        DWORD HighPart;
    } u;
#endif //MIDL_PASS
    ULONGLONG QuadPart;
} ULARGE_INTEGER;
    
typedef ULARGE_INTEGER *PULARGE_INTEGER;

BOOL GetDiskFreeSpaceExA(
  [in, optional]  LPCSTR          lpDirectoryName,
  [out, optional] PULARGE_INTEGER lpFreeBytesAvailableToCaller,
  [out, optional] PULARGE_INTEGER lpTotalNumberOfBytes,
  [out, optional] PULARGE_INTEGER lpTotalNumberOfFreeBytes
);

示例代码:

#include 
#include 

void main(void)
{
	WCHAR *dirName = L"D:\\test";
	ULARGE_INTEGER avail, total, totalFree;
	GetDiskFreeSpaceExW(dirName, &avail, &total, &totalFree);
	printf("%llu \n %llu\n %llu\n", avail.QuadPart, total.QuadPart, totalFree.QuadPart);
	return;
}

你可能感兴趣的:(Windows,windows,c++,开发语言)