简体   繁体   English

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

[英]How can i get the cpu information in .net?

例如奔腾或AMD等。

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();
                }

The System.Management Namespace Provides 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 Management Instrumentation(WMI)基础结构的系统,设备和应用程序有关。

The Win32 Processor WMI class represents a device that can interpret a sequence of instructions on a computer running on a Windows operating system. Win32 Processor WMI类代表一种设备,该设备可以解释Windows操作系统上运行的计算机上的指令序列。 On a multiprocessor computer, one instance of the Win32_Processor class exists for each processor. 在多处理器计算机上,每个处理器都存在Win32_Processor类的一个实例。 The class includes a Processor family type field, encoding things like AMD Opteron Processor Family . 该类包括一个“ Processor family type字段,对诸如AMD Opteron Processor Family的代码进行编码。

An example of C# issuing WMI query is at the end of the page. 页面末尾是C#发出WMI查询的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM