How can I read HDD volume serial number using VB.NET?

孤人 提交于 2020-01-05 10:57:35

问题


How can I read HDD volume serial number using VB.NET but without any third party addons for Visual Studio or any external libraries - I neeed native VB.NET code for this if possible.


回答1:


Not sure if this is what you're looking for but a quick google search lead me to this website:

http://www.vbgold.com/vb-projects/disk-serial-number.shtml




回答2:


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



回答3:


' Function driveser (model)
' Returns the serial number of the drive specified in "model" or an empty string. 
' Please include this is you are going to use it.
' (C) By Zibri 2013
' Free for non commercial use.
' zibri AT zibri DOT org

Function driveser(ByVal model As String) As String
    Dim devid As String = ""
    driveser = ""
    Try
        Dim searcher As New ManagementObjectSearcher( _
            "root\CIMV2", _
            "SELECT * FROM Win32_DiskDrive WHERE Model LIKE '%" + model + "%'")
        For Each queryObj As ManagementObject In searcher.Get()
            If queryObj("SerialNumber") <> "" Then driveser = queryObj("SerialNumber")
            Debug.Print(queryObj("Model") + ":" + driveser)
        Next
    Catch err As ManagementException
        Debug.Print("An error occurred while querying for WMI data: " & err.Message)
    End Try
End Function


来源:https://stackoverflow.com/questions/12101857/how-can-i-read-hdd-volume-serial-number-using-vb-net

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