开发者

Using WPF/C# to get information about the processor

I need to get basic information abou开发者_如何学Got the computer's processor in a WPF application I'm writing.

Data such as "Intel(R) core(TM) 2 Quad CPU Q6600 @ 2.4GHz"

How do I do this?


Use WMI

using System.Management;

private static string GetProcessorID()
    {

      ManagementClass mgt = new ManagementClass("Win32_Processor");
      ManagementObjectCollection procs= mgt.GetInstances();
        foreach ( ManagementObject item in procs)
             return item.Properties["Name"].Value.ToString();

        return "Unknown";
    }


This information (and much, much more) is available via Windows Management Instrumentation (or WMI for short). It isn't technically tied to WPF. Please take a look at this article to get you started!


Some of what you're looking for is exposed by properties of the System.Environment class. You might also be interested in the System.Windows.Forms.SystemInformation class.


Use WMI to obtain the information needed, especially the classes in the System.Management namespace. First. add a reference to the System.Management assembly, then use code similar to this one:

ManagementClass wmiManagementProcessorClass = new ManagementClass("Win32_Processor");
ManagementObjectCollection wmiProcessorCollection = wmiManagementProcessorClass.GetInstances();
foreach (ManagementObject wmiProcessorObject in wmiProcessorCollection)
{
    try
    {
        MessageBox.Show(wmiProcessorObject.Properties["Name"].Value.ToString());
    }
    catch (ManagementException ex)
    {
        // real error handling here
        MessageBox.Show(ex.Message);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜