C# 无法在WPF中获取系统标识符
我用WPF C#编程,并试图获取ProcessorID(或其他系统标识符)。我已经通读了。我添加了名称空间,但它不提供C# 无法在WPF中获取系统标识符,c#,wmi,system.management,C#,Wmi,System.management,我用WPF C#编程,并试图获取ProcessorID(或其他系统标识符)。我已经通读了。我添加了名称空间,但它不提供ManagementBaseObject类 using System.Management; /* code */ System.Management.(there is no ManagementBaseObject) System.Management是否仅在WinForms中使用,而不在WPF中使用?您需要添加对System.Management.dll的引用 (根据该
ManagementBaseObject类
using System.Management;
/* code */
System.Management.(there is no ManagementBaseObject)
System.Management是否仅在WinForms中使用,而不在WPF中使用?您需要添加对System.Management.dll的引用
(根据该类中的“程序集”)在
System.Core
中存在一些带有System.Management
命名空间的现有类型,这就是您看到某些类型的原因
但是,对于
ManagementBaseObject
,您还需要向项目中添加对System.Management.dll
的引用。以下代码将为您提供处理器id,因为您已经添加了对System.Management
的引用:
public static string GetProcessorID()
{
var processorID = "";
var query = "SELECT ProcessorId FROM Win32_Processor";
var oManagementObjectSearcher = new ManagementObjectSearcher(query);
foreach (var oManagementObject in oManagementObjectSearcher.Get())
{
processorID = (string)oManagementObject["ProcessorId"];
break;
}
return processorID;
}
Dirk的代码可能返回空对象。 请按以下方式更正:
public static string GetProcessorID()
{
string cpuid = "";
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
foreach (ManagementObject mo in mbs.Get())
{
var processorId = mo["ProcessorID"];
if (processorId != null)
{
cpuid = processorId.ToString();
break;
}
}
return cpuid;
}
@KMC:您不必这样做,但因为它是一个独立于任何对象状态的方法,所以它是有意义的。还要注意的是,
System.Management
命名空间完全独立于您的应用程序是控制台、Windows窗体还是WPF应用程序。非常感谢,必须使用“System.Management.dll”。