如何获取.net中的cpu信息?

问题描述 投票:0回答:4

比如是奔腾还是AMD等

c# .net vb.net
4个回答
12
投票

请注意,这是来自 VS2003:

using (ManagementObjectSearcher win32Proc = new ManagementObjectSearcher("select * from Win32_Processor")
{
    foreach (ManagementObject obj in win32Proc.Get())
    {
        clockSpeed = obj["CurrentClockSpeed"].ToString();
        procName = obj["Name"].ToString();
        manufacturer = obj["Manufacturer"].ToString();
        version = obj["Version"].ToString();
    }
}

3
投票

System.Management 命名空间 提供对有关 Windows Management Instrumentation (WMI) 基础结构的系统、设备和应用程序的丰富管理信息和管理事件的访问。

Win32 处理器 WMI 类表示可以解释运行 Windows 操作系统的计算机上的一系列指令的设备。在多处理器计算机上,每个处理器都存在一个 Win32_Processor 类的实例。该类包含一个 Processor family type

 字段,对诸如 
AMD Opteron Processor Family 之类的内容进行编码。

页面末尾有

C# 发出 WMI 查询的示例。


0
投票

此代码将获取CPU属性

Imports System.Management Private Sub InsertInfo() lstView.Items.Clear() Dim searcher As New ManagementObjectSearcher("select * from Win32_Processor") Try For Each share As ManagementObject In searcher.Get() Dim grp As ListViewGroup Try grp = lstView.Groups.Add(share("Name").ToString(), share("Name").ToString()) Catch grp = lstView.Groups.Add(share.ToString(), share.ToString()) End Try If share.Properties.Count <= 0 Then MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information) Return End If For Each PC As PropertyData In share.Properties Dim item As New ListViewItem(grp) If lstView.Items.Count Mod 2 <> 0 Then item.BackColor = Color.White Else item.BackColor = Color.WhiteSmoke End If item.Text = PC.Name If PC.Value IsNot Nothing AndAlso PC.Value.ToString().Length > 0 Then Select Case PC.Value.GetType().ToString() Case "System.String[]" Dim str As String() = DirectCast(PC.Value, String()) Dim str2 As String = "" For Each st As String In str str2 += st & " " Next item.SubItems.Add(str2) Exit Select Case "System.UInt16[]" Dim shortData As UShort() = DirectCast(PC.Value, UShort()) Dim tstr2 As String = "" For Each st As UShort In shortData tstr2 += st.ToString() & " " Next item.SubItems.Add(tstr2) Exit Select Case Else item.SubItems.Add(PC.Value.ToString()) Exit Select End Select Else Continue For End If lstView.Items.Add(item) Next Next Catch exp As Exception MessageBox.Show("can't get data because of the followeing error " & vbLf & exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information) End Try End Sub
    

0
投票
''' <summary> ''' Gets processor information. Returns a list of tuples consisting of; ''' 1*. the name of a processor. ''' 2*. a property name. ''' 3*. a corresponding property value. ''' </summary> ''' <param name="propertyChoice"> ''' If filled contains the name of the only property to return for each processor.</param> ''' <remarks> ''' This routine is inspired by "This code will get CPU properties" of boubkhaled. ''' </remarks> Public Shared Function getProcessorInformation( Optional propertyChoice As String = "") _ As List(Of Tuple(Of String, String, String)) ' EAHMK 20240519 Dim listOfTuples As New List(Of Tuple(Of String, String, String)) Dim processorName As String = String.Empty Dim propertyName As String = String.Empty Dim propertyValue As String = String.Empty propertyChoice = propertyChoice.ToUpper Dim wmiSearcher As New Management.ManagementObjectSearcher( queryString:="select * from Win32_Processor") For Each wmiProcessorInstance As Management.ManagementObject In wmiSearcher.Get() Try processorName = wmiProcessorInstance("Name").ToString() Catch processorName = wmiProcessorInstance.ToString() End Try For Each propertyData As Management.PropertyData In wmiProcessorInstance.Properties propertyName = propertyData.Name If String.IsNullOrEmpty(propertyChoice) Then ElseIf propertyName.ToUpper <> propertyChoice Then Continue For End If If propertyData.Value Is Nothing Then Continue For End If propertyValue = CType(propertyData.Value, String) Dim singleTuple As New Tuple(Of String, String, String) _ (item1:=processorName, item2:=propertyName, item3:=propertyValue) Call listOfTuples.Add(item:=singleTuple) Next Next Return listOfTuples End Function
    
© www.soinside.com 2019 - 2024. All rights reserved.