GetVolumeInformation() 函数原型如下:
[C++] 纯文本查看 复制代码 BOOL WINAPI GetVolumeInformation(
__in_opt LPCTSTR lpRootPathName,
__out LPTSTR lpVolumeNameBuffer,
__in DWORD nVolumeNameSize,
__out_opt LPDWORD lpVolumeSerialNumber,
__out_opt LPDWORD lpMaximumComponentLength,
__out_opt LPDWORD lpFileSystemFlags,
__out LPTSTR lpFileSystemNameBuffer,
__in DWORD nFileSystemNameSize
);
该函数用来获取卷 信息。
第 1 个参数 lpRootPathName 是输入参数,指向所要获取属性的驱动器的根路径字符串。
第 2 个参数 lpVolumeNameBuffer 是输出参数,返回卷标名称。注意,如果该磁盘分区没有卷标,在系统的资源管理器里会显示“本地磁盘”,但实际卷标为空。
第 3 个参数 nVolumeNameSize 是输入参数,表示卷标名称长度。
第 4 个参数 lpVolumeSerialNumber 是输出参数,用来存放驱动器的序列号。
第 5 个参数 lpMaximumComponentLength 是输出参数,返回文件系统支持的文件名最大长度。其中的 Component 是指 “ the portion of a file name between backslashes.”,即两个反斜杠直接的那部分,如 C:\Program Files (x86)\Common Files 中的 “Program Files (x86)” 。
第 6 个参数 lpFileSystemFlags 是输出参数,用一个整型数中的不同的位来指示驱动器的属性,比如可以判断是否支持“磁盘配额”, 是否支持“大小写不敏感的文件名搜索”,是否支持“文件加密“功能等等。
第 7 个参数 lpFileSystemNameBuffer 是输出参数,表示文件系统类型,如 NTFS,FAT32 等。
第 8 个参数 nFileSystemNameSize 表示的是 lpFileSystemNameBuffer 缓冲区的大小。
如果函数返回非零值表示成功获取相关信息,否则失败。
测试代码:
[C++] 纯文本查看 复制代码 #include "stdafx.h"
#define MaxLength 1024
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "chs"); //为了下面打印能正常显式中文,进行了本地化设置
TCHAR VolumeNameBuffer[MAX_PATH];
DWORD VolumeSerialNumber;
DWORD MaximumComponentLength;
DWORD FileSystemFlags;
TCHAR FileSystemNameBuffer[MaxLength];
TCHAR Buffer[MaxLength];
GetLogicalDriveStrings(MaxLength, Buffer);
PTSTR Device = (PTSTR)Buffer;
do {
if ( !GetVolumeInformation(
Device,
VolumeNameBuffer,
MAX_PATH,
&VolumeSerialNumber,
&MaximumComponentLength,
&FileSystemFlags,
FileSystemNameBuffer,
MaxLength
)
) {
return FALSE;
}
_tprintf (TEXT("%s\n"),Device);
_tprintf (TEXT("磁盘驱动器卷标名称:%s\n"), VolumeNameBuffer);
_tprintf (TEXT("磁盘驱动器卷标序列号: %u\n"), VolumeSerialNumber);
_tprintf (TEXT("系统允许最大文件名长度:%u\n"), MaximumComponentLength);
_tprintf (TEXT("文件系统类型:%s\n"), FileSystemNameBuffer);
if (FileSystemFlags & FILE_CASE_PRESERVED_NAMES)
_tprintf (TEXT("The specified volume supports preserved case of file names when it places a name on disk.\n"));
if (FileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
_tprintf (TEXT("The specified volume supports case-sensitive file names.\n"));
if (FileSystemFlags & FILE_FILE_COMPRESSION)
_tprintf (TEXT("The specified volume supports file-based compression.\n"));
if (FileSystemFlags & FILE_NAMED_STREAMS)
_tprintf (TEXT("The specified volume supports named streams.\n"));
if (FileSystemFlags & FILE_VOLUME_QUOTAS)
_tprintf (TEXT("The specified volume supports disk quotas.\n"));
_tprintf (TEXT("------------------------------------------------------------------\n"));
Device += (lstrlen((LPCWSTR)Device) + 1);
} while (*Device);
return 0;
}
运行输出:C:\
磁盘驱动器卷标名称:
磁盘驱动器卷标序列号: 3103004495
系统允许最大文件名长度:255
文件系统类型:NTFS
The specified volume supports preserved case of file names when it places a name on disk.
The specified volume supports case-sensitive file names.
The specified volume supports file-based compression.
The specified volume supports named streams.
The specified volume supports disk quotas.
------------------------------------------------------------------
D:\
磁盘驱动器卷标名称:新加卷
磁盘驱动器卷标序列号: 1625376328
系统允许最大文件名长度:255
文件系统类型:NTFS
The specified volume supports preserved case of file names when it places a name on disk.
The specified volume supports case-sensitive file names.
The specified volume supports file-based compression.
The specified volume supports named streams.
The specified volume supports disk quotas.
------------------------------------------------------------------
E:\
磁盘驱动器卷标名称:WinSoft
磁盘驱动器卷标序列号: 1856715788
系统允许最大文件名长度:255
文件系统类型:NTFS
The specified volume supports preserved case of file names when it places a name on disk.
The specified volume supports case-sensitive file names.
The specified volume supports file-based compression.
The specified volume supports named streams.
The specified volume supports disk quotas.
------------------------------------------------------------------ |