风遁.阿修罗之螺旋丸:读取硬件信息的类(VB.NET原创)
来源:百度文库 编辑:九乡新闻网 时间:2024/03/28 17:58:26
读取硬件信息的类(VB.NET原创)
程序相关 2007-10-05 10:28:30 阅读698 评论1 字号:大中小 订阅
Public Class HardWare
'*************************************************************************
'定义数据结构
'主板数据结构
Public Structure HW_MainBoard
Private m_Name As String '名称
Private m_SerialNumber As String '序列号
Private m_Manufacturer As String '制造商
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public Property SerialNumber() As String
Get
SerialNumber = m_SerialNumber
End Get
Set(ByVal value As String)
m_SerialNumber = value
End Set
End Property
Public Property Manufacturer() As String
Get
Manufacturer = m_Manufacturer
End Get
Set(ByVal value As String)
m_Manufacturer = value
End Set
End Property
End Structure
'CPU数据结构
Public Structure HW_Cpu
Private m_Name As String '名称
Private m_ProcessorId As String '生产批号
Private m_MaxClockSpeed As String '最大频率
Private m_Manufacturer As String '制造商
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public Property ProcessorId() As String
Get
ProcessorId = m_ProcessorId
End Get
Set(ByVal value As String)
m_ProcessorId = value
End Set
End Property
Public Property MaxClockSpeed() As String
Get
MaxClockSpeed = m_MaxClockSpeed
End Get
Set(ByVal value As String)
m_MaxClockSpeed = value
End Set
End Property
Public Property Manufacturer() As String
Get
Manufacturer = m_Manufacturer
End Get
Set(ByVal value As String)
m_Manufacturer = value
End Set
End Property
End Structure
'内存数据结构
Public Structure HW_Memory
Private m_Capacity As String '容量
Private m_Speed As String '最大频率
Private m_Manufacturer As String '制造商
Public Property Capacity() As String
Get
Capacity = m_Capacity
End Get
Set(ByVal value As String)
m_Capacity = value
End Set
End Property
Public Property Speed() As String
Get
Speed = m_Speed
End Get
Set(ByVal value As String)
m_Speed = value
End Set
End Property
Public Property Manufacturer() As String
Get
Manufacturer = m_Manufacturer
End Get
Set(ByVal value As String)
m_Manufacturer = value
End Set
End Property
End Structure
'硬盘数据结构
Public Structure HW_DiskDrive
Private m_SerialNumber As String '序列号
Private m_Size As String '容量
Private m_Model As String '模板
Private m_Manufacturer As String '制造商
Public Property SerialNumber() As String
Get
SerialNumber = m_SerialNumber
End Get
Set(ByVal value As String)
m_SerialNumber = value
End Set
End Property
Public Property Size() As String
Get
Size = m_Size
End Get
Set(ByVal value As String)
m_Size = value
End Set
End Property
Public Property Model() As String
Get
Model = m_Model
End Get
Set(ByVal value As String)
m_Model = value
End Set
End Property
Public Property Manufacturer() As String
Get
Manufacturer = m_Manufacturer
End Get
Set(ByVal value As String)
m_Manufacturer = value
End Set
End Property
End Structure
'网卡数据结构
Public Structure HW_NetWork
Private m_Name As String '网卡名称
Private m_NetConnectionID As String '连接ID
Private m_MAC As String 'MAC地址
Private m_IPAddress As String 'IP
Private m_IPSubnet As String '子网掩码
Private m_DefaultIPGateway As String '网关
Private m_ServiceName As String '生产序号
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public Property NetConnectionID() As String
Get
NetConnectionID = m_NetConnectionID
End Get
Set(ByVal value As String)
m_NetConnectionID = value
End Set
End Property
Public Property MAC() As String
Get
MAC = m_MAC
End Get
Set(ByVal value As String)
m_MAC = value
End Set
End Property
Public Property IPAddress() As String
Get
IPAddress = m_IPAddress
End Get
Set(ByVal value As String)
m_IPAddress = value
End Set
End Property
Public Property IPSubnet() As String
Get
IPSubnet = m_IPSubnet
End Get
Set(ByVal value As String)
m_IPSubnet = value
End Set
End Property
Public Property DefaultIPGateway() As String
Get
DefaultIPGateway = m_DefaultIPGateway
End Get
Set(ByVal value As String)
m_DefaultIPGateway = value
End Set
End Property
Public Property ServiceName() As String
Get
ServiceName = m_ServiceName
End Get
Set(ByVal value As String)
m_ServiceName = value
End Set
End Property
End Structure
'逻辑磁盘数据结构
Public Structure HW_LogicalDisk
Private m_Name As String ' 磁盘名
Private m_DeviceID As String '磁盘ID
Private m_VolumeName As String '磁盘卷标
Private m_VolumeSerialNumber As String '磁盘序列号码
Private m_FileSystem As String '磁盘格式
Private m_Size As String '磁盘大小
Private m_Description As String '磁盘描述
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public Property DeviceID() As String
Get
DeviceID = m_DeviceID
End Get
Set(ByVal value As String)
m_DeviceID = value
End Set
End Property
Public Property VolumeName() As String
Get
VolumeName = m_VolumeName
End Get
Set(ByVal value As String)
m_VolumeName = value
End Set
End Property
Public Property VolumeSerialNumber() As String
Get
VolumeSerialNumber = m_VolumeSerialNumber
End Get
Set(ByVal value As String)
m_VolumeSerialNumber = value
End Set
End Property
Public Property FileSystem() As String
Get
FileSystem = m_FileSystem
End Get
Set(ByVal value As String)
m_FileSystem = value
End Set
End Property
Public Property Size() As String
Get
Size = m_Size
End Get
Set(ByVal value As String)
m_Size = value
End Set
End Property
Public Property Description() As String
Get
Description = m_Description
End Get
Set(ByVal value As String)
m_Description = value
End Set
End Property
End Structure
'********************************************************************************
'定义变量
'存储查到的信息
Private m_Time As Date '读取数据的时间
Private m_MainBoard As HW_MainBoard '主板数据
Private m_Cpu(-1) As HW_Cpu 'cpu数据
Private m_Memory(-1) As HW_Memory '内存
Private m_DiskDrive(-1) As HW_DiskDrive '硬盘
Private m_NetWork(-1) As HW_NetWork '网卡
Private m_FloppyDrive(-1) As HW_LogicalDisk '软盘
Private m_DiskPartition(-1) As HW_LogicalDisk '磁盘分区
Private m_CDROM(-1) As HW_LogicalDisk '光驱
Private m_NetworkDrive(-1) As HW_LogicalDisk '网络驱动器
'**************************************************************************
'定义属性
'取得硬件数据的时间
Public ReadOnly Property Time() As Date
Get
Try
Return m_Time
Catch ex As Exception
Return Now
End Try
End Get
End Property
'主板
Public ReadOnly Property MainBoard() As HW_MainBoard
Get
Try
Return m_MainBoard
Catch ex As Exception
MsgBox("读取主板错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_MainBoard")
Return Nothing
End Try
End Get
'Set(ByVal Value As HW_MainBoard)
' m_MainBoard = Value
'End Set
End Property
'cpu
Public ReadOnly Property Cpu(ByVal index As Integer) As HW_Cpu
Get
Try
If index < 0 Or index > m_Cpu.Length - 1 Or m_Cpu.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_CPU")
Return New HW_Cpu
Exit Property
End If
Return m_Cpu(index)
Catch ex As Exception
MsgBox("读取CPU错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_CPU")
Return Nothing
End Try
End Get
'Set(ByVal Value As HW_Cpu)
' m_Cpu(index) = Value
'End Set
End Property
'内存
Public ReadOnly Property Memory(ByVal index As Integer) As HW_Memory
Get
Try
If index < 0 Or index > m_Memory.Length - 1 Or m_Memory.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_Memory")
Return New HW_Memory
Exit Property
End If
Return m_Memory(index)
Catch ex As Exception
MsgBox("读取内存错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_Memory")
Return Nothing
End Try
End Get
'Set(ByVal Value As HW_Memory)
' m_Memory(index) = Value
'End Set
End Property
'硬盘
Public ReadOnly Property DiskDrive(ByVal index As Integer) As HW_DiskDrive
Get
Try
If index < 0 Or index > m_DiskDrive.Length - 1 Or m_DiskDrive.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_DiskDrive")
Return New HW_DiskDrive
Exit Property
End If
Return m_DiskDrive(index)
Catch ex As Exception
MsgBox("读取硬盘错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_DiskDrive")
Return Nothing
End Try
End Get
'Set(ByVal Value As HW_DiskDrive)
' m_DiskDrive(index) = Value
'End Set
End Property
'网卡
Public ReadOnly Property NetWork(ByVal index As Integer) As HW_NetWork
Get
Try
If index < 0 Or index > m_NetWork.Length - 1 Or m_NetWork.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_NetWork")
Return New HW_NetWork
Exit Property
End If
Return m_NetWork(index)
Catch ex As Exception
MsgBox("读取网卡错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_NetWork")
Return Nothing
End Try
End Get
'Set(ByVal Value As HW_NetWork)
' m_NetWork(index) = Value
'End Set
End Property
'软驱
Public ReadOnly Property FloppyDrive(ByVal index As Integer) As HW_LogicalDisk
Get
Try
If index < 0 Or index > m_FloppyDrive.Length - 1 Or m_FloppyDrive.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_FloppyDrive")
Return New HW_LogicalDisk
Exit Property
End If
Return m_FloppyDrive(index)
Catch ex As Exception
MsgBox("读取软驱错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_FloppyDrive")
Return Nothing
End Try
End Get
End Property
'磁盘驱动器
Public ReadOnly Property DiskPartition(ByVal index As Integer) As HW_LogicalDisk
Get
Try
If index < 0 Or index > m_DiskPartition.Length - 1 Or m_DiskPartition.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_DiskPartition")
Return New HW_LogicalDisk
Exit Property
End If
Return m_DiskPartition(index)
Catch ex As Exception
MsgBox("读取磁盘驱动器错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_DiskPartition")
Return Nothing
End Try
End Get
End Property
'光盘
Public ReadOnly Property CDROM(ByVal index As Integer) As HW_LogicalDisk
Get
Try
If index < 0 Or index > m_CDROM.Length - 1 Or m_CDROM.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_CDROM")
Return New HW_LogicalDisk
Exit Property
End If
Return m_CDROM(index)
Catch ex As Exception
MsgBox("读取光盘驱动器错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_CDROM")
Return Nothing
End Try
End Get
End Property
'网络驱动器
Public ReadOnly Property NetworkDrive(ByVal index As Integer) As HW_LogicalDisk
Get
Try
If index < 0 Or index > m_NetworkDrive.Length - 1 Or m_NetworkDrive.Length = 0 Then
MsgBox("索引超出范围", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_NetworkDrive")
Return New HW_LogicalDisk
Exit Property
End If
Return m_NetworkDrive(index)
Catch ex As Exception
MsgBox("读取网络驱动器错误,返回'Nothing'值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_NetworkDrive")
Return Nothing
End Try
End Get
End Property
'光驱数量
Public ReadOnly Property CDROMCount() As Integer
Get
Return m_CDROM.Length
End Get
End Property
'磁盘驱动器数量
Public ReadOnly Property DiskPartitionCount() As Integer
Get
Return m_DiskPartition.Length
End Get
End Property
'软驱数量
Public ReadOnly Property FloppyDriveCount() As Integer
Get
Return m_FloppyDrive.Length
End Get
End Property
'网络驱动器数量
Public ReadOnly Property NetworkDriveCount() As Integer
Get
Return m_NetworkDrive.Length
End Get
End Property
'cpu数量
Public ReadOnly Property CpuCount() As Integer
Get
Return m_Cpu.Length
End Get
End Property
'内存数量
Public ReadOnly Property MemoryCount() As Integer
Get
Return m_Memory.Length
End Get
End Property
'硬盘数量
Public ReadOnly Property DiskDriveCount() As Integer
Get
Return m_DiskDrive.Length
End Get
End Property
'网卡数量
Public ReadOnly Property NetWorkCount() As Integer
Get
Return m_NetWork.Length
End Get
End Property
'**************************************************************************
'定义方法
'初始化类
Public Sub New()
m_Time = Now()
Refresh()
End Sub
'刷新数据
Public Sub Refresh()
Try
GetMainBoard()
GetCpu()
GetMemory()
GetNetWork()
GetDiskDrive()
GetLogicalDisk()
m_Time = Now()
Catch ex As Exception
'MsgBox("在刷新时产生错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_Refresh")
End Try
End Sub
'对取得的数据进行处理,转换成String类型
Private Function DP(ByVal Value As Object) As String
Dim StrValue As String
StrValue = ""
Try
Select Case VarType(Value)
Case VariantType.Object
StrValue = ""
'Case VariantType.String
' StrValue = Trim(Value)
Case VariantType.Array + VariantType.String
Dim i = 0
For i = 0 To Value.length - 1
StrValue = StrValue & Value(i)
Next
'Case VariantType.Integer
' StrValue = CStr(Value)
Case Else
StrValue = CStr(Value)
End Select
Return Trim(StrValue)
Catch ex As Exception
MsgBox("在处理数据时产生错误,返回‘’值。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_DP")
Return ""
End Try
End Function
'读取主板数据
Private Sub GetMainBoard()
Try
Dim MainBoardClass As New System.Management.ManagementObjectSearcher("select * from Win32_BIOS")
Dim MainBoard As System.Management.ManagementObject
For Each MainBoard In MainBoardClass.Get()
m_MainBoard.Name = DP(MainBoard("Name"))
m_MainBoard.SerialNumber = DP(MainBoard("SerialNumber"))
m_MainBoard.Manufacturer = DP(MainBoard("Manufacturer"))
Next
MainBoard = Nothing
MainBoardClass = Nothing
Catch ex As Exception
MsgBox("读取主板错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_GetMainBoard")
End Try
End Sub
'读取Cpu数据
Private Sub GetCpu()
Try
Dim CpuClass As New System.Management.ManagementObjectSearcher("select * from Win32_Processor")
Dim Cpu As System.Management.ManagementObject
Dim i As Integer = -1
For Each Cpu In CpuClass.Get()
i = i + 1
ReDim Preserve m_Cpu(i)
m_Cpu(i).Name = DP(Cpu("Name"))
m_Cpu(i).ProcessorId = DP(Cpu("ProcessorId"))
m_Cpu(i).MaxClockSpeed = DP(Cpu("MaxClockSpeed"))
m_Cpu(i).Manufacturer = DP(Cpu("Manufacturer"))
Next
Cpu = Nothing
CpuClass = Nothing
Catch ex As Exception
MsgBox("读取CPU错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_GetCpu")
End Try
End Sub
'读取内存数据
Private Sub GetMemory()
Try
Dim MemoryClass As New System.Management.ManagementObjectSearcher("select * from Win32_PhysicalMemory")
Dim Memory As System.Management.ManagementObject
Dim i As Integer = -1
For Each Memory In MemoryClass.Get()
i = i + 1
ReDim Preserve m_Memory(i)
m_Memory(i).Capacity = DP(Int((Memory("Capacity") / 1024 / 1024) * 100) / 100)
m_Memory(i).Speed = DP(Memory("Speed"))
m_Memory(i).Manufacturer = DP(Memory("Manufacturer"))
Next
Memory = Nothing
MemoryClass = Nothing
Catch ex As Exception
MsgBox("读取内存错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_GetMemory")
End Try
End Sub
'读取活动网卡数据
Private Sub GetNetWork()
Try
Dim NetWorkClass_One As New System.Management.ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration")
Dim NetWork_One As System.Management.ManagementObject
Dim NetWorkClass_Two As New System.Management.ManagementObjectSearcher("select * from Win32_NetworkAdapter")
Dim NetWork_Two As System.Management.ManagementObject
Dim i As Integer = -1
For Each NetWork_One In NetWorkClass_One.Get()
If (NetWork_One("IPEnabled")) Then
i = i + 1
ReDim Preserve m_NetWork(i)
m_NetWork(i).MAC = DP(NetWork_One("MACAddress"))
'MsgBox(NetWork_One("IPAddress"))
m_NetWork(i).IPAddress = DP(NetWork_One("IPAddress"))
m_NetWork(i).IPSubnet = DP(NetWork_One("IPSubnet"))
m_NetWork(i).DefaultIPGateway = DP(NetWork_One("DefaultIPGateway"))
m_NetWork(i).ServiceName = DP(NetWork_One("ServiceName"))
For Each NetWork_Two In NetWorkClass_Two.Get()
If m_NetWork(i).MAC = DP(NetWork_Two("MACAddress")) Then
m_NetWork(i).Name = DP(NetWork_Two("Name"))
m_NetWork(i).NetConnectionID = DP(NetWork_Two("NetConnectionID"))
End If
Next
End If
Next
NetWork_One = Nothing
NetWorkClass_One = Nothing
NetWork_Two = Nothing
NetWorkClass_Two = Nothing
Catch ex As Exception
MsgBox("读取网卡错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_GetNetWork")
End Try
End Sub
'读取硬盘数据
Private Sub GetDiskDrive()
Try
Dim DiskDriveClass As New System.Management.ManagementObjectSearcher("select * from Win32_PhysicalMedia")
Dim DiskDrive As System.Management.ManagementObject
Dim i As Integer = -1
For Each DiskDrive In DiskDriveClass.Get()
i = i + 1
ReDim Preserve m_DiskDrive(i)
m_DiskDrive(i).SerialNumber = DP(DiskDrive("SerialNumber"))
'm_DiskDrive(i).Model = DiskDrive("Model")
'm_DiskDrive(i).Manufacturer = DiskDrive("Manufacturer")
Next
DiskDriveClass = New System.Management.ManagementObjectSearcher("select * from Win32_DiskDrive")
i = -1
For Each DiskDrive In DiskDriveClass.Get()
i = i + 1
'm_DiskDrive(i).SerialNumber = DiskDrive("SerialNumber")
m_DiskDrive(i).Model = DP(DiskDrive("Model"))
m_DiskDrive(i).Manufacturer = DP(DiskDrive("Manufacturer"))
m_DiskDrive(i).Size = DP(Int((DiskDrive("Size") / 1024 / 1024 / 1024) * 100) / 100)
Next
DiskDrive = Nothing
DiskDriveClass = Nothing
Catch ex As Exception
MsgBox("读取硬盘错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_GetDiskDrive")
End Try
End Sub
'读取逻辑驱动器
Private Sub GetLogicalDisk()
Try
Dim LogicalDiskClass As New System.Management.ManagementObjectSearcher("select * from Win32_LogicalDisk")
Dim LogicalDisk As System.Management.ManagementObject
'Dim i As Integer = -1
For Each LogicalDisk In LogicalDiskClass.Get()
Select Case LogicalDisk("DriveType")
Case 2
SetDrive(m_FloppyDrive, LogicalDisk)
Case 3
SetDrive(m_DiskPartition, LogicalDisk)
Case 4
SetDrive(m_NetworkDrive, LogicalDisk)
Case 5
SetDrive(m_CDROM, LogicalDisk)
End Select
Next
LogicalDisk = Nothing
LogicalDiskClass = Nothing
Catch ex As Exception
MsgBox("读取磁盘驱动器错误:" & Chr(13) & ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "SystemDetect_GetLogicalDisk")
End Try
End Sub
'添加新的驱动器
Private Sub SetDrive(ByRef ArrayName() As HW_LogicalDisk, ByVal DiskData As System.Management.ManagementObject)
ReDim Preserve ArrayName(ArrayName.Length)
ArrayName(ArrayName.Length - 1).Name = DP(DiskData("Name"))
ArrayName(ArrayName.Length - 1).DeviceID = DP(DiskData("DeviceID"))
ArrayName(ArrayName.Length - 1).VolumeName = DP(DiskData("VolumeName"))
ArrayName(ArrayName.Length - 1).VolumeSerialNumber = DP(DiskData("VolumeSerialNumber"))
ArrayName(ArrayName.Length - 1).FileSystem = DP(DiskData("FileSystem"))
ArrayName(ArrayName.Length - 1).Size = DP(DiskData("Size"))
ArrayName(ArrayName.Length - 1).Description = DP(DiskData("Description"))
End Sub
End Class