获取所有硬盘和分区的序列号

我想获得所有物理硬盘序列号(不是驱动器的批量序列号)和分区。

其实我用DISKID32来实现所有的硬盘序列号,但是这不会给硬盘分区。所以我打算使用其他一些方法。

下面的代码给出了获取物理硬盘的序列号,并在每个硬盘上find分区。

ComputerName = "." Set wmiServices = GetObject ( _ "winmgmts:{impersonationLevel=Impersonate}!//" _ & ComputerName) ' Get physical disk drive Set wmiDiskDrives = wmiServices.ExecQuery ( _ "SELECT * FROM Win32_DiskDrive") For Each wmiDiskDrive In wmiDiskDrives MsgBox "Disk drive Caption: " _ & wmiDiskDrive.Caption _ & VbNewLine & "DeviceID: " _ & " (" & wmiDiskDrive.DeviceID & ")" MsgBox "Serial number" _ & wmiDiskDrive.SerialNumber 'Use the disk drive device id to ' find associated partition query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _ & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" Set wmiDiskPartitions = wmiServices.ExecQuery(query) For Each wmiDiskPartition In wmiDiskPartitions 'Use partition device id to find logical disk Set wmiLogicalDisks = wmiServices.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _ & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") For Each wmiLogicalDisk In wmiLogicalDisks MsgBox "Drive letter associated" _ & " with disk drive = " _ & wmiDiskDrive.Caption _ & wmiDiskDrive.DeviceID _ & VbNewLine & " Partition = " _ & wmiDiskPartition.DeviceID _ & VbNewLine & " is " _ & wmiLogicalDisk.DeviceID Next Next Next 

它在Windows 8中完美的作品。但是,当我在Windows XP中testing时,我得到了序列号错误,如wmiDiskDrive.SerialNumber。 所有其他对象正常工作。

然后我发现这个属性是不可用在Windows XP中,Windows Server 2003等现在从上面的代码我可以得到硬盘型号和分区,但我想要的序列号。

那么我如何获得硬盘序列号和他们的分区(应该在所有的Windows操作系统)? 任何想法?

这应该解决你的问题..

 Public Function GetDriveSerialNumber(DrvL As String) As Long Dim fso As Object, Drv As Object Dim driveletter As String Dim DriveSerial As String Set fso = CreateObject("Scripting.FileSystemObject") driveletter = DrvL Set Drv = fso.GetDrive(driveletter) With Drv If .IsReady Then DriveSerial = Abs(.SerialNumber) Else DriveSerial = -1 End If End With Set Drv = Nothing Set fso = Nothing GetDriveSerialNumber = DriveSerial End Function