-- 作者:有点甜
-- 发布时间:2018/1/24 11:43:00
--
mark 硬件信息
全局代码
Public Class commonUtlity Public Shared Function ToGB(ByVal size As Double, ByVal m As Double) As String Dim units As String() = New String() {"B", "KB", "MB", "GB", "TB", "PB"} Dim i As Integer = 0 Do While size >= M size /= M i+=1 Loop Return Math.Round(size) & units(i) End Function End Class
Public Enum WindowsAPIKeys Name VideoProcessor AdapterRAM ScreenWidth ScreenHeight Version Size Capacity NumberOfCores End Enum
Public Enum WindowsAPIType Win32_PhysicalMemory Win32_Processor win32_DiskDrive Win32_ComputerSystemProduct Win32_DesktopMonitor Win32_VideoController Win32_OperatingSystem End Enum
Public Class Computer
Private Shared _instance As Computer
Private Shared ReadOnly _lock As Object = New Object()
Private Sub New() End Sub
Public Shared Function CreateComputer() As Computer If _instance Is Nothing Then SyncLock _lock If _instance Is Nothing Then _instance = New Computer() End If End SyncLock End If
Return _instance End Function
Public Function GetCPU() As Dictionary(Of String, String) Dim result As new Dictionary(Of String, String) Try Dim str As String = String.Empty Dim mcCPU As System.Management.ManagementClass = New System.Management.ManagementClass(WindowsAPIType.Win32_Processor.ToString()) Dim mocCPU As System.Management.ManagementObjectCollection = mcCPU.GetInstances() For Each m As System.Management.ManagementObject In mocCPU Dim name As String = m(WindowsAPIKeys.Name.ToString()).ToString() Dim parts As String() = name.Split("@"c) result.add(parts(0).Split("-"c)(0) & "处理器", parts(1)) Exit For Next Catch End Try
Return result End Function
Public Function GetCPU_Count() As String Dim str As String = "查询失败" Try Dim coreCount As Integer = 0 For Each item As object In New System.Management.ManagementObjectSearcher("Select * from " & WindowsAPIType.Win32_Processor.ToString()).[Get]() coreCount += Integer.Parse(item(WindowsAPIKeys.NumberOfCores.ToString()).ToString()) Next If coreCount = 2 Then Return "双核" End If str = coreCount.ToString() & "核" Catch End Try
Return str End Function
Public Function GetPhisicalMemory() As String Dim searcher As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher() searcher.Query = New System.Management.SelectQuery(WindowsAPIType.Win32_PhysicalMemory.ToString(), "", New String() {WindowsAPIKeys.Capacity.ToString()}) Dim collection As System.Management.ManagementObjectCollection = searcher.[Get]() Dim em As System.Management.ManagementObjectCollection.ManagementObjectEnumerator = collection.GetEnumerator() Dim capacity As Long = 0 While em.MoveNext() Dim baseObj As object= em.Current If baseObj.Properties(WindowsAPIKeys.Capacity.ToString()).Value IsNot Nothing Then Try capacity += Long.Parse(baseObj.Properties(WindowsAPIKeys.Capacity.ToString()).Value.ToString()) Catch Return "查询失败" End Try End If End While
Return CommonUtlity.ToGB(CDbl(capacity), 1024) End Function
Public Function GetDiskSize() As String Dim result As String = String.Empty Dim sb As StringBuilder = New StringBuilder() Try Dim hdId As String = String.Empty Dim hardDisk As System.Management.ManagementClass = New System.Management.ManagementClass(WindowsAPIType.win32_DiskDrive.ToString()) Dim hardDiskC As System.Management.ManagementObjectCollection = hardDisk.GetInstances() For Each m As System.Management.ManagementObject In hardDiskC Dim capacity As Long = Convert.ToInt64(m(WindowsAPIKeys.Size.ToString()).ToString()) sb.Append(CommonUtlity.ToGB(capacity, 1000) & "+") Next result = sb.ToString().TrimEnd("+"c) Catch End Try
Return result End Function
Public Function GetVersion() As String Dim str As String = "查询失败" Try Dim hdId As String = String.Empty Dim hardDisk As System.Management.ManagementClass = New System.Management.ManagementClass(WindowsAPIType.Win32_ComputerSystemProduct.ToString()) Dim hardDiskC As System.Management.ManagementObjectCollection = hardDisk.GetInstances() For Each m As System.Management.ManagementObject In hardDiskC str = m(WindowsAPIKeys.Version.ToString()).ToString() Exit For Next Catch End Try
Return str End Function
Public Function GetFenbianlv() As String Dim result As String = "1920*1080" Try Dim hdId As String = String.Empty Dim hardDisk As System.Management.ManagementClass = New System.Management.ManagementClass(WindowsAPIType.Win32_DesktopMonitor.ToString()) Dim hardDiskC As System.Management.ManagementObjectCollection = hardDisk.GetInstances() For Each m As System.Management.ManagementObject In hardDiskC result = m(WindowsAPIKeys.ScreenWidth.ToString()).ToString() & "*" + m(WindowsAPIKeys.ScreenHeight.ToString()).ToString() Exit For Next Catch End Try
Return result End Function
Public Function GetVideoController() As Dictionary(Of String, String) Dim result As new Dictionary(Of String, String) Try Dim hardDisk As System.Management.ManagementClass = New System.Management.ManagementClass(WindowsAPIType.Win32_VideoController.ToString()) Dim hardDiskC As System.Management.ManagementObjectCollection = hardDisk.GetInstances() For Each m As System.Management.ManagementObject In hardDiskC result.add(m(WindowsAPIKeys.VideoProcessor.ToString()).ToString().Replace("Family", ""), CommonUtlity.ToGB(Convert.ToInt64(m(WindowsAPIKeys.AdapterRAM.ToString()).ToString()), 1024)) Exit For Next Catch End Try
Return result End Function
Public Function GetOS_Version() As String Dim str As String = "Windows 10" Try Dim hdId As String = String.Empty Dim hardDisk As System.Management.ManagementClass = New System.Management.ManagementClass(WindowsAPIType.Win32_OperatingSystem.ToString()) Dim hardDiskC As System.Management.ManagementObjectCollection = hardDisk.GetInstances() For Each m As System.Management.ManagementObject In hardDiskC str = m(WindowsAPIKeys.Name.ToString()).ToString().Split("|"c)(0).Replace("Microsoft", "") Exit For Next Catch End Try
Return str End Function End Class
调用代码
Dim dic = Computer.CreateComputer.getCPU For Each key As String In dic.keys output.show(key & ":" & dic(key)) Next dic = Computer.CreateComputer.GetVideoController For Each key As String In dic.keys output.show(key & ":" & dic(key)) Next Dim str = Computer.CreateComputer.GetCPU_Count output.show(str) str = Computer.CreateComputer.GetPhisicalMemory output.show(str) str = Computer.CreateComputer.GetDiskSize output.show(str) str = Computer.CreateComputer.GetVersion output.show(str)
str = Computer.CreateComputer.GetFenbianlv output.show(str) str = Computer.CreateComputer.GetOS_Version output.show(str)
添加dll引用
此主题相关图片如下:qq截图20180124114152.png
|