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

时间:2008-12-04 12:05:04

标签: c# .net vb.net

无论是奔腾还是AMD等。

3 个答案:

答案 0 :(得分:12)

请注意,这是来自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();
                }

答案 1 :(得分:3)

System.Management Namespace 提供对Windows Management Instrumentation(WMI)基础结构中配备的系统,设备和应用程序的丰富管理信息和管理事件的访问。

Win32 Processor WMI类表示可以解释在Windows操作系统上运行的计算机上的指令序列的设备。在多处理器计算机上,每个处理器都存在一个Win32_Processor类实例。该类包含一个Processor family type字段,用于编码 AMD Opteron处理器系列等内容。

C# issuing WMI query的示例位于页面的末尾。

答案 2 :(得分: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