C# 我如何在 .net 中获取 cpu 信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/340359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get the cpu information in .net?
提问by suhair
like whether it is pentium or AMD etc.
比如是奔腾还是AMD等。
回答by gimel
The System.Management NamespaceProvides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure.
该System.Management命名空间提供访问一组丰富的有关系统,设备和仪表的Windows管理规范(WMI)基础设施应用的管理信息和管理事件。
The Win32 ProcessorWMI class represents a device that can interpret a sequence of instructions on a computer running on a Windows operating system. On a multiprocessor computer, one instance of the Win32_Processor class exists for each processor. The class includes a Processor family type
field, encoding things like AMD Opteron Processor Family.
所述的Win32处理器WMI类表示可以在Windows操作系统上的计算机上运行的解释指令序列的装置。在多处理器计算机上,每个处理器都存在 Win32_Processor 类的一个实例。该类包括一个Processor family type
字段,对诸如AMD Opteron Processor Family之类的内容进行编码。
An example of C# issuing WMI queryis at the end of the page.
C# 发出 WMI 查询的示例位于页面末尾。
回答by WACM161
Please note that this is from VS2003:
请注意,这是来自 VS2003:
using(ManagementObjectSearcher win32Proc = new ManagementObjectSearcher("select * from Win32_Processor"),
win32CompSys = new ManagementObjectSearcher("select * from Win32_ComputerSystem"),
win32Memory = new ManagementObjectSearcher("select * from Win32_PhysicalMemory"))
{
foreach (ManagementObject obj in win32Proc.Get())
{
clockSpeed = obj["CurrentClockSpeed"].ToString();
procName = obj["Name"].ToString();
manufacturer = obj["Manufacturer"].ToString();
version = obj["Version"].ToString();
}
回答by KhaledDev
This code will get CPU properties
此代码将获取 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