-1

我正在尝试使用 Visual Basic 2010 获取卷序列号,

是否有完整的代码示例向我展示如何执行此操作?

谢谢

4

2 回答 2

3

我想我的问题最简单的答案是:

Hans Passant:从他的链接,

我只是复制并粘贴了这个函数,它适用于 Microsoft Visual basic 2010 express,没有任何修改

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

我要感谢大家的帮助,

我很抱歉重复这个问题,我确实做了谷歌搜索和堆栈流搜索,但我的搜索是“在 Visual Basic 2010 中获取硬盘序列号”

所以这个网站没有出现,

再次感谢

于 2013-09-04T16:14:04.317 回答
1

这个线程在这里http://social.msdn.microsoft.com/Forums/vstudio/en-US/43281cfa-51c8-4c35-bc31-929c67abd943/getting-drive-volume-serial-number-in-vb-2010有以下您可以使用/调整的代码。

我为您制作了一段代码来显示所有驱动器信息。

包含卷序列号,您可以通过简单地在代码中添加更多 if 来获得它

Imports System.Management
Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim drivetype() As String = {"Unknown", "NoRootDirectory", _
    "RemoveableDisk", "LocalDisk", "NetworkDrive", "CompactDisk", "RamDisk"}
    Dim allDrives() As String = Environment.GetLogicalDrives()
    For Each drive In allDrives
      Dim win32Drive As String = _
      "Win32_LogicalDisk='" & drive.Substring(0, 2) & "'"
      Dim Disk As System.Management.ManagementObject _
      = New System.Management.ManagementObject(win32Drive)
      Me.ListBox1.Items.Add(drive.ToString & drivetype(CInt((Disk("DriveType").ToString))))
      For Each diskProperty In Disk.Properties
        If Not diskProperty.Value Is Nothing Then
          Me.ListBox1.Items.Add("---" & diskProperty.Name & "=" & diskProperty.Value.ToString)
        End If
      Next
    Next
  End Sub
End Class
于 2013-09-03T23:22:09.343 回答