C# net核心:ManagementObjectSearcher在初始化时引发异常
我正在尝试使用ManagementObjectSearcher运行一个查询这是我的代码:net core 2.1C# net核心:ManagementObjectSearcher在初始化时引发异常,c#,.net-core,C#,.net Core,我正在尝试使用ManagementObjectSearcher运行一个查询这是我的代码:net core 2.1 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * from Win32_Printer"); ManagementObjectCollection coll = searcher.Get(); 在我的计算机windows 10上工作正常,但在客户端计算机windows Se
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * from Win32_Printer");
ManagementObjectCollection coll = searcher.Get();
在我的计算机windows 10上工作正常,但在客户端计算机windows Server 2012R2上,我遇到以下错误:
`Unhandled Exception: System.TypeInitializationException: The type initializer fo
r 'System.Management.ManagementPath' threw an exception. ---> System.TypeInitial
izationException: The type initializer for 'System.Management.WmiNetUtilsHelper'
threw an exception. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: ptr
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPt
r ptr, Type t)
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDele
gate](IntPtr ptr)
at System.Management.WmiNetUtilsHelper.LoadDelegate[TDelegate](TDelegate& del
egate_f, IntPtr hModule, String procName)
at System.Management.WmiNetUtilsHelper..cctor()
--- End of inner exception stack trace ---
at System.Management.MTAHelper.IsNoContextMTA()
at System.Management.MTAHelper.CreateInMTA(Type type)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..ctor(String path)
at System.Management.ManagementPath..cctor()
--- End of inner exception stack trace ---
at System.Management.ManagementScope._Clone(ManagementScope scope, Identifier
ChangedEventHandler handler)
at System.Management.ManagementObjectSearcher..ctor(ManagementScope scope, Ob
jectQuery query, EnumerationOptions options)`
我将其发布为win-86 exe,似乎是一个系统。管理需要完整的.NET框架。此特定查询似乎仅适用于.NET Framework 4.7。我已经用4.0、4.5.2和4.7.2对它进行了测试,只有后者不会给我这个异常。我的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WMINet\u Utils.dll版本是4.7.2053.0
附带说明,Microsoft不鼓励新代码使用System.Management,而是使用Microsoft.Management.Infrastructure。似乎System.Management需要完整的.NET框架。此特定查询似乎仅适用于.NET Framework 4.7。我已经用4.0、4.5.2和4.7.2对它进行了测试,只有后者不会给我这个异常。我的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WMINet\u Utils.dll版本是4.7.2053.0
旁注:Microsoft不鼓励新代码使用System.Management,而使用Microsoft.Management.Infrastructure。如何发布为win-86 exe?如何发布为win-86 exe?Nuget软件包有什么内容?Nuget软件包有什么内容?