大侠,把硬盘物理序列号的VB源代码 也发给我下,着急啊,谢谢,80806471@qq.com

 我来答
wifel
2011-01-07 · 超过17用户采纳过TA的回答
知道答主
回答量:54
采纳率:0%
帮助的人:37.1万
展开全部
  Imports System.Management
  Imports System.IO
  Imports ErrPlug = CC.PublicTools.StrEdit.ErrorStr

  Partial Public Class HardWare

  Public Sub New()
  MyBase.New()
  End Sub

  Public Class HardDiskInfo

  Public Sub New()
  MyBase.New()
  End Sub

  Public Class HardDrive

  Public Sub New()
  MyBase.New()
  End Sub

  ''' <summary>
  ''' 返回单个的硬盘信息。
  ''' </summary>
  ''' <remarks></remarks>
  Public Class _HardDrive

  Public Sub New()
  MyBase.New()
  End Sub

  Public DriveID As Integer
  Public TotalSize As String
  Public DiskTypes As String
  Public DiskModel As String
  Public FingerKey As String
  Public Overloads Function ToString() As String
  Return String.Format("DiskID:{0}|Type:{1}|Model:{2}|Size:{3}|FigSn:{4}", DriveID, DiskTypes, DiskModel, TotalSize, FingerKey)
  End Function
  End Class

  ''' <summary>
  ''' 执行获取磁盘硬盘信息的过程函数。
  ''' </summary>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Shared Function DoGetHardDriveInfo() As List(Of _HardDrive)
  Dim ResultDriveList As List(Of _HardDrive) = New List(Of _HardDrive)
  Try
  Dim query As New SelectQuery("Win32_DiskDrive")
  Using search As New ManagementObjectSearcher(query)
  Dim ITempa As Integer = 0
  For Each info In search.Get()
  ITempa += 1
  Application.DoEvents()
  ' ResultDiskList.AllDiskCount += 1
  Dim NewDisk As _HardDrive = New _HardDrive() With {.DriveID = ITempa}
  If info("TotalSectors") IsNot Nothing Then
  NewDisk.TotalSize = StrEdit.NumConvert.FormatBytes(CDbl((CType(info("TotalSectors"), UInt64) * CType(info("BytesPerSector"), UInt32))))
  Else
  NewDisk.TotalSize = "Unknown"
  End If
  If info("InterfaceType") IsNot Nothing Then
  NewDisk.DiskTypes = info("InterfaceType").ToString
  Else
  NewDisk.DiskTypes = "Unknown"
  End If
  If info("Model") IsNot Nothing Then
  NewDisk.DiskModel = info("Model").ToString
  Else
  NewDisk.DiskModel = "Unknown"
  End If
  If info("signature") IsNot Nothing Then
  NewDisk.FingerKey = info("signature").ToString
  Else
  NewDisk.FingerKey = "Unknown"
  End If
  ResultDriveList.Add(NewDisk)
  Next
  End Using
  Catch ex As Exception
  ErrPlug.WriteErrorLog(ex)
  End Try
  Return ResultDriveList
  End Function

  End Class

  Public Class DiskPart

  Public Sub New()
  MyBase.New()
  End Sub

  Public Class _DiskPart

  Public Sub New()
  MyBase.New()
  End Sub

  Public PartLetter As String
  Public PartType As String
  Public PartFileSystem As String
  Public PartIsReady As Boolean
  Public PartLabel As String
  Public PartTotalSize As String
  Public PartUsedSize As String
  Public PartFreeSize As String
  Public PartSerialNo As String
  Public PartFressPersent As String
  Public Overloads Function ToString() As String
  Return "Name:" & PartLetter & "|Type:" & PartType & "|F.S.:" & PartFileSystem & "|IsReady:" & PartIsReady & "|Label:" & PartLabel & _
  "|Total:" & PartTotalSize & "|Used:" & PartUsedSize & "|Free:" & PartFreeSize & "|SN:" & PartSerialNo & "|FreeP:" & PartFressPersent
  End Function
  End Class

  ''' <summary>
  ''' 执行返回磁盘分区信息的列表。
  ''' </summary>
  ''' <remarks></remarks>
  Public Shared Function DoGetDiskPartList() As List(Of _DiskPart)
  Dim ResultPartList As List(Of _DiskPart) = New List(Of _DiskPart)

  Try
  Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
  Dim CurDriver As DriveInfo

  For Each CurDriver In allDrives
  Application.DoEvents()

  Dim NewPart As _DiskPart = New _DiskPart() With {.PartLetter = CurDriver.RootDirectory.ToString, .PartType = CurDriver.DriveType.ToString}
  If CurDriver.IsReady = True Then
  NewPart.PartFileSystem = CurDriver.DriveFormat
  Else
  NewPart.PartFileSystem = ""
  End If
  NewPart.PartIsReady = CurDriver.IsReady
  If CurDriver.IsReady = True Then
  NewPart.PartLabel = CurDriver.VolumeLabel
  Else
  NewPart.PartLabel = ""
  End If
  If CurDriver.IsReady = True Then
  NewPart.PartTotalSize = StrEdit.NumConvert.FormatBytes(CDbl(CurDriver.TotalSize))
  Else
  NewPart.PartTotalSize = ""
  End If
  If CurDriver.IsReady = True Then
  NewPart.PartFreeSize = StrEdit.NumConvert.FormatBytes(CDbl(CurDriver.TotalFreeSpace))
  Else
  NewPart.PartFreeSize = ""
  End If
  If CurDriver.IsReady = True Then
  NewPart.PartUsedSize = StrEdit.NumConvert.FormatBytes(CDbl(CurDriver.TotalSize - CurDriver.TotalFreeSpace))
  Else
  NewPart.PartUsedSize = ""
  End If
  If CurDriver.IsReady = True Then
  NewPart.PartFressPersent = String.Format("{0}%", FormatNumber(CDbl(CurDriver.TotalFreeSpace / CurDriver.TotalSize * 100), 1))
  Else
  NewPart.PartFressPersent = "0.00%"
  End If
  If CurDriver.IsReady = True Then
  NewPart.PartSerialNo = GetVolumeSerialNumber(CurDriver.RootDirectory.ToString)
  Else
  NewPart.PartSerialNo = ""
  End If
  ResultPartList.Add(NewPart)
  Next

  Catch ex As Exception
  ErrPlug.WriteErrorLog(ex)
  End Try
  Return ResultPartList
  End Function

  End Class

  ''' <summary>
  ''' 获取硬盘序列号。
  ''' </summary>
  ''' <param name="lpRootPathName"></param>
  ''' <param name="lpVolumeNameBuffer"></param>
  ''' <param name="nVolumeNameSize"></param>
  ''' <param name="lpVolumeSerialNumber"></param>
  ''' <param name="lpMaximumComponentLength"></param>
  ''' <param name="lpFileSystemFlags"></param>
  ''' <param name="lpFileSystemNameBuffer"></param>
  ''' <param name="nFileSystemNameSize"></param>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" _
  (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, _
  ByVal nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Integer, _
  ByRef lpMaximumComponentLength As Integer, ByRef lpFileSystemFlags As Integer, _
  ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Integer) As Integer

  ''' <summary>
  ''' 获取硬盘序列号
  ''' </summary>
  ''' <param name="volume">volume "drive letter"</param>
  Public Shared Function GetVolumeSerialNumber(ByVal volume As String) As String
  Dim check As Integer
  Dim volumeSerialNumber As Integer
  Dim unused As String
  Dim volumeName As String

  Try
  ' Pad the strings.
  volumeName = Space(14)
  unused = Space(32)
  check = GetVolumeInformation(volume, volumeName, Len(volumeName), _
  volumeSerialNumber, 0, 0, unused, Len(unused))
  ' Error check.
  If check = 0 Then
  Return ""
  Else
  Return Hex(volumeSerialNumber)
  End If
  Catch
  Return ""
  End Try
  Return ""
  End Function

  End Class

  End Class
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式