Any faster method to get Volume Serial number?

橙三吉。 提交于 2019-12-13 14:42:56

问题


Hi I'm using this code to generate machine signature. But it's take noticeable time to execute. Wonder why it's that slow ? Any faster method recommended ?

Public Shared Function DriveSN(ByVal DriveLetter As String) As String
    Dim disk As ManagementObject = New ManagementObject(String.Format("Win32_Logicaldisk='{0}'", DriveLetter))
    Dim VolumeName As String = disk.Properties("VolumeName").Value.ToString()
    Dim SerialNumber As String = disk.Properties("VolumeSerialnumber").Value.ToString()
    Return SerialNumber.Insert(4, "-")
End Function

Private Shared msig As String = Nothing

Public Shared Function MachineSignature() As String
    If msig Is Nothing Then
        Dim list As New List(Of String)
        For Each d As DriveInfo In DriveInfo.GetDrives()
            If (d.IsReady) Then
                list.Add(DriveSN(d.Name.Substring(0, 2)))
            End If
        Next
        msig = String.Join(" & ", list.ToArray())
    End If
    Return msig
End Function

回答1:


There is a .Net way also from egghead cafe using the System.Management namespace.

Firstly, you need to add the reference to the System.Management dll in vb.net. Using the Project->Add Reference menu item.

Then the following code gets the drive serial number for the C drive:

Dim drive As String = "C"
Dim disk As System.Management.ManagementObject = _
       New System.Management.ManagementObject _
       ("win32_logicaldisk.deviceid=""" + drive + ":""")
disk.Get()
Dim SerialNumber as String = disk("VolumeSerialNumber").ToString()



回答2:


This is embarrassing. The performance problem can be solved by simply check for "fixed" drive.

Public Shared Function MachineSignature() As String
    If msig Is Nothing Then
        Dim list As New List(Of String)
        For Each d As DriveInfo In DriveInfo.GetDrives()
            If (d.DriveType = DriveType.Fixed) AndAlso (d.IsReady) Then  ' <-- check for DriveType'
                list.Add(DriveSN(d.Name.Substring(0, 2)))
            End If
        Next
        msig = String.Join(" & ", list.ToArray())
    End If
    Return msig
End Function



回答3:


There is a Win32 API call to this also, but i think WMI is the better way since it's still managed code.

Win32 API function: GetVolumeInformation

I found the following function on eggheadcafe (it's C#, but should be not problem to do in vb.net) to extract the Serial number:

public string GetVolumeSerial(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256); // Label
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256); // File System Name
strDriveLetter+=":\\"; // fix up the passed-in drive letter for the API call
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);

return Convert.ToString(serNum);
}


来源:https://stackoverflow.com/questions/1142579/any-faster-method-to-get-volume-serial-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!