Mit dieser kleinen Klasse kann man auf einfacher weise ein bestimmtes Hardware Objekt auslesen und alle Eigenschaften dieses Objekts in einer Dictionary Collection speichern. Die Eigenschaften kann man dann aus der Dictionary Collection einfach abrufen in dem man als Key den Namen der Eigenschaft angibt wie z.B. "Caption" oder "Name"
Beispiel:
Dim DiskDrive As Collections.Generic.Dictionary(Of String, String)
DiskDrive = WMI.Hardware.GetProperties(WMI.Hardware.Win32._DiskDrive)
MsgBox(DiskDrive("Caption"))
PS: Ein Verweis auf die DLL System.Management muss vorher hinzugefügt werden.
Imports System.Management
Public Class WMI
Public Class Hardware
Public Shared Function GetProperties(ByVal [Class] As Win32) As Collections.Generic.Dictionary(Of String, String())
Dim MOS As ManagementObjectSearcher = New ManagementObjectSearcher("Select * from Win32" & [Class].ToString)
Dim MOC As ManagementObjectCollection = MOS.Get
Dim MO As ManagementObject
Dim PD As PropertyData
Dim HWList As New Collections.Generic.Dictionary(Of String, String())
For Each MO In MOC
For Each PD In MO.Properties
If Not PD.Value Is Nothing Then
If HWList.ContainsKey(PD.Name) = False Then
HWList.Add(PD.Name, GetProperty([Class], PD.Name))
End If
End If
Next
Next
Return HWList
End Function
Public Shared Function GetProperty(ByVal [Class] As Win32, ByVal [Property] As String) As String()
Dim MOS As ManagementObjectSearcher = New ManagementObjectSearcher("Select " & [Property] & " from Win32" & [Class].ToString)
Dim MOC As ManagementObjectCollection = MOS.Get
Dim MO As New ManagementObject
Dim Values As New List(Of String)
Try
For Each MO In MOC
Values.Add(MO.GetPropertyValue([Property]))
Next
Catch ex As Exception
Return Nothing
End Try
Return Values.ToArray
End Function
Public Enum Win32 As Integer
'Cooling Device Classes
_Fan
_HeatPipe
_Refrigeration
_TemperatureProbe
'Input Device Classes
_Keyboard
_PointingDevice
'Mass Storage Classes
_AutochkSetting
_CDROMDrive
_DiskDrive
_FloppyDrive
_PhysicalMedia
_TapeDrive
'Motherboard, Controller, and Port Classes
_1394Controller
_1394ControllerDevice
_AllocatedResource
_AssociatedProcessorMemory
_BaseBoard
_BIOS
_Bus
_CacheMemory
_ControllerHasHub
_DeviceBus
_DeviceMemoryAddress
_DeviceSettings
_DMAChannel
_FloppyController
_IDEController
_IDEControllerDevice
_InfraredDevice
_IRQResource
_MemoryArray
_MemoryArrayLocation
_MemoryDevice
_MemoryDeviceArray
_MemoryDeviceLocation
_MotherboardDevice
_OnBoardDevice
_ParallelPort
_PCMCIAController
_PhysicalMemory
_PhysicalMemoryArray
_PhysicalMemoryLocation
_PNPAllocatedResource
_PNPDevice
_PNPEntity
_PortConnector
_PortResource
_Processor
_SCSIController
_SCSIControllerDevice
_SerialPort
_SerialPortConfiguration
_SerialPortSetting
_SMBIOSMemory
_SoundDevice
_SystemBIOS
_SystemDriverPNPEntity
_SystemEnclosure
_SystemMemoryResource
_SystemSlot
_USBController
_USBControllerDevice
_USBHub
'Networking Device Classes
_NetworkAdapter
_NetworkAdapterConfiguration
_NetworkAdapterSetting
'Power Classes
_AssociatedBattery
_Battery
_CurrentProbe
_PortableBattery
_PowerManagementEvent
_UninterruptiblePowerSupply
_VoltageProbe
'Printing Classes
_DriverForDevice
_Printer
_PrinterConfiguration
_PrinterController
_PrinterDriver
_PrinterDriverDll
_PrinterSetting
_PrintJob
_TCPIPPrinterPort
'Telephony Classes
_POTSModem
_POTSModemToSerialPort
'Video and Monitor Classes
_DesktopMonitor
_DisplayConfiguration
_DisplayControllerConfiguration
_VideoConfiguration
_VideoController
_VideoSettings
End Enum
End Class
End Class
4 Kommentare zum Snippet