簡體   English   中英

net core:ManagementObjectSearcher在初始化時拋出異常

[英]net core: ManagementObjectSearcher throws exception on initializing

我試圖用ManagementObjectSearcher運行查詢這里是我的代碼:( net core 2.1)

 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

似乎System.Management需要完整的.NET Framework。 此特定查詢似乎只適用於.NET Framework 4.7。 我用4.0,4.5.2和4.7.2進行了測試,只有后者不會給我這個例外。 我的C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\WMINet_Utils.dll版本是4.7.2053.0

請注意,Microsoft不鼓勵使用System.Management新代碼,而是使用Microsoft.Management.Infrastructure

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM