项目场景:
报错:
System.Management currently is only supported for Windows desktop applications.
Cannot marshal ‘parameter #3’: Cannot marshal a string by-value with the [Out] attribute.
#region GetCpuInfo 获取cpu序列号
/// <summary>
/// 获取cpu序列号
/// </summary>
/// <returns> string </returns>
public static string GetCpuInfo()
{
var cpuInfo = "";
try
{
using (var cimobject = new ManagementClass("Win32_Processor"))
{
var moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
mo.Dispose();
}
}
}
catch (Exception)
{
throw;
}
return cpuInfo.ToString();
}
#endregion
问题描述
v7.0.0
System.PlatformNotSupportedException
HResult=0x80131539
Message=System.Management currently is only supported for Windows desktop applications.
Source=System.Management
v4.5.0
System.Runtime.InteropServices.MarshalDirectiveException
HResult=0x80131535
Message=Cannot marshal ‘parameter #3’: Cannot marshal a string by-value with the [Out] attribute.
Source=System.Private.CoreLib
StackTrace:
在 System.StubHelpers.StubHelpers.ThrowInteropParamException(Int32 resID, Int32 paramIdx)
在 System.Management.ManagementPath.GetWbemPath(IWbemPath wbemPath)
在 System.Management.ManagementPath.GetWbemPath()
在 System.Management.ManagementPath.get_Path()
在 System.Management.ManagementPath.get_IsEmpty()
在 System.Management.ManagementObject.ManagementObjectCTOR(ManagementScope scope, ManagementPath path, ObjectGetOptions options)
在 System.Management.ManagementObject…ctor(ManagementScope scope, ManagementPath path, ObjectGetOptions options)
在 System.Management.ManagementClass…ctor(ManagementScope scope, ManagementPath path, ObjectGetOptions options)
在 System.Management.ManagementClass…ctor(String path)
原因分析:
显然易得
解决方案:
中间的4.6.0-6.0.0都可正常使用