如何使用Visual Basic 2010获取卷序列号?

时间:2013-09-03 22:28:01

标签: visual-studio-2010

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

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

由于

2 个答案:

答案 0 :(得分: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

我要感谢大家的帮助,

我为重复这个问题而道歉, 我做了谷歌搜索和stackflow搜索, 但我的搜索是“ “在visual basic 2010中获取硬盘序列号”

所以这个网站没有出现,

再次感谢

答案 1 :(得分:1)

此处的帖子http://social.msdn.microsoft.com/Forums/vstudio/en-US/43281cfa-51c8-4c35-bc31-929c67abd943/getting-drive-volume-serial-number-in-vb-2010包含以下您可以使用/适应的代码。

  

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

     

包含卷序列号,您可以通过简单的方式获得   在代码中添加更多内容

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