使用System.Management获取cpu序列号出现异常

项目场景:

报错:
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都可正常使用
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值