无法在.net核心桌面应用程序中获取硬件信息



我想获得cpu和主板(以及一段时间的硬件(id所以我使用以下代码:

public class SystemKey : ISystemKey
{
public string GetDeviceId(int Length)
{
var uniqueKey = GetDeviceId();
if (uniqueKey.Length >= Length)
return uniqueKey.Substring(0, Length);
var key = new StringBuilder(uniqueKey);
for (int counter = uniqueKey.Length; counter <= Length; counter++)
{
var i = Length - uniqueKey.Length;
key.Append(uniqueKey[i]);
}
return key.ToString();
}
public string GetDeviceId()
{
string uniqueKey; 
try
{
uniqueKey = GetCpuSerialNumbewr() + GetMotherBoardID();
}
catch
{
ISystemKey alternativeSystemKey = new AlternativeSystemKey();
uniqueKey = alternativeSystemKey.GetDeviceId(); 
}
if(string.IsNullOrEmpty(uniqueKey))
{
throw new Exception("can not get device id for current system"); 
}
return uniqueKey;
}
private string GetCpuSerialNumbewr()
{
List<string> results = new List<string>();
string query = "Select * FROM Win32_Processor";
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
foreach (ManagementObject info in searcher.Get())
{
results.Add(
info.GetPropertyValue("ProcessorId").ToString());
}
return results.FirstOrDefault();
}
private string GetMotherBoardID()
{
string mbInfo = String.Empty;
//Get motherboard's serial number 
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
foreach (ManagementObject mo in mbs.Get())
{
mbInfo += mo["SerialNumber"].ToString();
}
return mbInfo;
}
}

和备用系统密钥(在调用的上述代码中(:

public class AlternativeSystemKey : ISystemKey
{
private static string identifier(string wmiClass, string wmiProperty)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
return result;
}
private static string cpuId()
{
//Uses first CPU identifier available in order of preference
//Don't get all identifiers, as it is very time consuming
string retVal = identifier("Win32_Processor", "UniqueId");
if (retVal == "") //If no UniqueID, use ProcessorID
{
retVal = identifier("Win32_Processor", "ProcessorId");
if (retVal == "") //If no ProcessorId, use Name
{
retVal = identifier("Win32_Processor", "Name");
if (retVal == "") //If no Name, use Manufacturer
{
retVal = identifier("Win32_Processor", "Manufacturer");
}
//Add clock speed for extra security
retVal += identifier("Win32_Processor", "MaxClockSpeed");
}
}
return retVal;
}
//BIOS Identifier
private static string biosId()
{
return identifier("Win32_BIOS", "Manufacturer")
+ identifier("Win32_BIOS", "SMBIOSBIOSVersion")
+ identifier("Win32_BIOS", "IdentificationCode")
+ identifier("Win32_BIOS", "SerialNumber")
+ identifier("Win32_BIOS", "ReleaseDate")
+ identifier("Win32_BIOS", "Version");
}
//Main physical hard drive ID
private static string diskId()
{
return identifier("Win32_DiskDrive", "Model")
+ identifier("Win32_DiskDrive", "Manufacturer")
+ identifier("Win32_DiskDrive", "Signature")
+ identifier("Win32_DiskDrive", "TotalHeads");
}
//Motherboard ID
private static string baseId()
{
return identifier("Win32_BaseBoard", "Model")
+ identifier("Win32_BaseBoard", "Manufacturer")
+ identifier("Win32_BaseBoard", "Name")
+ identifier("Win32_BaseBoard", "SerialNumber");
}
//Primary video controller ID
private static string videoId()
{
return identifier("Win32_VideoController", "DriverVersion")
+ identifier("Win32_VideoController", "Name");
}
public string GetDeviceId(int length)
{
throw new NotImplementedException();
}
public string GetDeviceId()
{
return baseId() + cpuId() + biosId();
}
}

因此,在这两个类中,我们有一些方法,但当我们在某些系统中使用它们时,它们不起作用(异常(,错误为:;microsoft.net\framework\v4.03019\wminet_utils没有具有所有必需的功能。请更新.net框架";但问题是:;为什么这个例外指控者?因为应用程序是用.net内核编写的并且作为自包含发布,这并不意味着应用程序应该在没有任何依赖性的情况下工作?摘要问题:

  1. 是否有任何兼容的.net核心代码用于获取硬件信息?2( 为什么当应用程序作为独立发布时,它会说:";我需要更多的依赖性来安装"?3( 解决上述问题的最佳方法(srry表示英语不好(

我刚刚尝试在.Net6中引用System.Managementnuget包,它没有问题。

事实上,看看6.0版本的软件包本身,它有一个.Net 6和.Net Core 3.1的版本。当然,你没有提到你引用了什么版本,但考虑到你得到的错误,你可能链接到了版本5.0,它没有.Net Core 3+版本(你也没有提到你试图在哪个.Net版本下运行它(。因此,它很可能绑定到.Net Framework版本,而其他系统上可能存在也可能不存在.Net Framework版本。

我们没有找到任何解决方案,但我们是如何解决的?实际上,当我们创建一个.net框架应用程序并尝试获取硬件信息时,没有问题(代码在.net core(system.management版本4/5/6(中不起作用(,所以我们创建了一个.net framework控制台应用程序,仅用于获取硬件信息,并在新的过程中调用该应用程序,并读取.net框架控制台应用程序新行结果,代码示例:

protected string GetDeviceId(int length)
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "WpfApp.IdGenerator.exe",
Arguments = $"{length}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
return proc.StandardOutput.ReadLine();
}

最新更新