我正在使用GetDriveSerialNumber()在visual basic 2010中检索驱动器卷序列号。
我有以下导入:
Imports System
Imports System.IO
Imports System.Text
Imports System.Net.NetworkInformation
Imports System.Management
Imports System.Collections
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
然后在模块中:
Dim path As String
'Get HD volume Serial Number from Function
Dim DriveVID As String = GetDriveSerialNumber()
功能是:
'Get HD Volume serial Number
Public Function GetDriveSerialNumber() As String
Dim DriveSerial As Long
Dim fso As Object, Drv As Object
'Create a FileSystemObject object
fso = CreateObject("Scripting.FileSystemObject")
Drv = fso.GetDrive(fso.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
With Drv
If .IsReady Then
DriveSerial = .SerialNumber
Else '"Drive Not Ready!"
DriveSerial = -1
End If
End With
'Clean up
Drv = Nothing
fso = Nothing
GetDriveSerialNumber = Hex(DriveSerial)
End Function
这适用于9台10台电脑,
只有一个给了我不同的7个字符,
反对它应该给我的8个字符,
即使我在cmd中输入vol命令,
在那台电脑上,
它为我提供了正确的8个字符的卷序列号,
有谁知道什么是错的?
答案 0 :(得分:0)
此功能似乎适用于我的所有测试计算机:
Private Function GetDriveSerialNumber(ByVal drive As String) As String
Dim driveSerial As String = String.Empty
Dim driveFixed As String = Path.GetPathRoot(drive)
driveFixed = Replace(driveFixed, "\", String.Empty)
Using querySearch As New ManagementObjectSearcher("SELECT VolumeSerialNumber FROM Win32_LogicalDisk Where Name = '" & driveFixed & "'")
Using queryCollection As ManagementObjectCollection = querySearch.Get()
Dim moItem As ManagementObject
For Each moItem In queryCollection
driveSerial = CStr(moItem.Item("VolumeSerialNumber"))
Exit For
Next
End Using
End Using
Return driveSerial
End Function
唯一的问题是你必须向函数发送驱动器号, 这对我来说不是问题,