【问题标题】:How can I read HDD volume serial number using VB.NET?如何使用 VB.NET 读取硬盘卷序列号?
【发布时间】:2012-08-19 13:44:39
【问题描述】:

如何使用 VB.NET 读取 HDD 卷序列号,但没有 Visual Studio 的任何第三方插件或任何外部库 - 如果可能,我需要本机 VB.NET 代码。

【问题讨论】:

  • 问题会自动迁移。请不要交叉发布。

标签: hard-drive vb.net


【解决方案1】:
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

【讨论】:

  • 这适用于我测试的 10 台计算机中的 9 台,只有一台给了我一个奇怪的不同的 7 个字符,反对它应该给我的 8 个字符,即使我在 cmd 中键入 vol 命令,在那电脑,它给了我正确的 8 个字符的卷序列号,
【解决方案2】:

不确定这是否是您要查找的内容,但通过 google 快速搜索将我引导至此网站:

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

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2011-05-10
      • 2010-11-24
      • 2014-12-31
      • 1970-01-01
      • 2010-12-06
      • 2016-06-10
      • 2011-05-04
      • 2019-03-27
      • 2014-01-11
      相关资源
      最近更新 更多