如何获取我的磁盘分区的 GUID?
How can I get the GUID of my disc partitions?
我正在使用 WMI 获取所有磁盘信息,如驱动器、驱动器号等,但我还没有找到如何获取每个分区的 UUID/GUID。
有几个 WMI 类 可以访问 Disks 和 Partitions 信息:
- Win32_Volume
- Win32_DiskDrive
- Win32_DiskPartition
还有这两个,它们将前面的相互联系起来:
- Win32_DiskDrive物理媒体
- Win32_DiskDriveToDiskPartition
特别是使用 Win32_Volume
您可以访问现有分区的 GUID。
您可以从 shell:
进行测试
> wmic volume get Driveletter, DeviceID
你得到这样的数据:
使用 delphi,您可以使用一些组件,或像这样的一个代码(来自 Code Creator -Rodrigo Ruz-):
program GetWMI_Info;
{$APPTYPE CONSOLE}
uses
SysUtils, ActiveX, ComObj, Variants;
procedure GetWin32_VolumeInfo;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = [=11=]000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Volume','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(Format('Description %s',[String(VarToStrDef(FWbemObject.Description, ''))]));// String
Writeln(Format('DeviceID %s',[String(VarToStrDef(FWbemObject.DeviceID, ''))]));// String
Writeln(Format('DriveLetter %s',[String(VarToStrDef(FWbemObject.DriveLetter, ''))]));// String
Writeln(Format('DriveType %d',[Integer(VarToStrDef(FWbemObject.DriveType, ''))]));// Uint32
Writeln(Format('Label %s',[String(VarToStrDef(FWbemObject.Label, ''))]));// String
Writeln(Format('Name %s',[String(VarToStrDef(FWbemObject.Name, ''))]));// String
Writeln(Format('SerialNumber %d',[Integer(VarToStrDef(FWbemObject.SerialNumber, ''))]));// Uint32
Writeln('');
FWbemObject:=Unassigned;
end;
end;
begin
try
CoInitialize(nil);
try
GetWin32_VolumeInfo;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
结果如下所示:
我正在使用 WMI 获取所有磁盘信息,如驱动器、驱动器号等,但我还没有找到如何获取每个分区的 UUID/GUID。
有几个 WMI 类 可以访问 Disks 和 Partitions 信息:
- Win32_Volume
- Win32_DiskDrive
- Win32_DiskPartition
还有这两个,它们将前面的相互联系起来:
- Win32_DiskDrive物理媒体
- Win32_DiskDriveToDiskPartition
特别是使用 Win32_Volume
您可以访问现有分区的 GUID。
您可以从 shell:
> wmic volume get Driveletter, DeviceID
你得到这样的数据:
使用 delphi,您可以使用一些组件,或像这样的一个代码(来自 Code Creator -Rodrigo Ruz-):
program GetWMI_Info;
{$APPTYPE CONSOLE}
uses
SysUtils, ActiveX, ComObj, Variants;
procedure GetWin32_VolumeInfo;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = [=11=]000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Volume','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(Format('Description %s',[String(VarToStrDef(FWbemObject.Description, ''))]));// String
Writeln(Format('DeviceID %s',[String(VarToStrDef(FWbemObject.DeviceID, ''))]));// String
Writeln(Format('DriveLetter %s',[String(VarToStrDef(FWbemObject.DriveLetter, ''))]));// String
Writeln(Format('DriveType %d',[Integer(VarToStrDef(FWbemObject.DriveType, ''))]));// Uint32
Writeln(Format('Label %s',[String(VarToStrDef(FWbemObject.Label, ''))]));// String
Writeln(Format('Name %s',[String(VarToStrDef(FWbemObject.Name, ''))]));// String
Writeln(Format('SerialNumber %d',[Integer(VarToStrDef(FWbemObject.SerialNumber, ''))]));// Uint32
Writeln('');
FWbemObject:=Unassigned;
end;
end;
begin
try
CoInitialize(nil);
try
GetWin32_VolumeInfo;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
结果如下所示: