C# ManagementObjectSearcher(“从Win32中选择*)=>NullReferenceException

C# ManagementObjectSearcher(“从Win32中选择*)=>NullReferenceException,c#,nullreferenceexception,dependency-management,C#,Nullreferenceexception,Dependency Management,我用语言C为我的应用程序编写了一个控制台应用程序,该语言带有运行低规格计算机所必需的框架dotnetcoreapp2.0。我知道警告,但它是windows功能和需求所必需的。我已经从C:\Windows\Microsoft.NET\Framework\v2.0.50727在Visual Basic上添加了项目引用,并添加了 v4.0.30319。我安装了dotnetcoreapp2.0运行时及其sdk。问题是,每当我与管理层调用静态类时。。编程时,调试后System.NullReferenceE

我用语言C为我的应用程序编写了一个控制台应用程序,该语言带有运行低规格计算机所必需的框架dotnetcoreapp2.0。我知道警告,但它是windows功能和需求所必需的。我已经从C:\Windows\Microsoft.NET\Framework\v2.0.50727在Visual Basic上添加了项目引用,并添加了

v4.0.30319。我安装了dotnetcoreapp2.0运行时及其sdk。问题是,每当我与管理层调用静态类时。。编程时,调试后System.NullReferenceException发生错误。我会提供程序

>1.using System; 
>2.using System.Collections.Generic; 
>3.using System.Text;
>4.using System.Management; 
>5.using System.IO; 
>6.using System.Windows; 
>7.using System.Linq; 
>8.using System.Collections; 
>9.using System.Data; 
>10.using System.ComponentModel; 
>11.using System.Windows.Forms;
>12. 
>13.namespace USBConsoleMonitor 
>14.{  
>15.class All_Driver_Existence 
>16.{  
>17.public static void Device_ALL() 
>18.{ 
>19.try 
>20.{ 
>21.ObjectQuery yep = new ObjectQuery("SELECT * FROM Win32_USBHub"); 
>22.ManagementScope yen = new ManagementScope("\\root\\cimv2");//default 
>23.ManagementObjectSearcher yes = new ManagementObjectSearcher(yen, yep);
>24.
>25.foreach (ManagementObject yesnt in yes.Get()) 
>26.{ 
>27.Console.WriteLine(yesnt["Name"]); 
>28.} 
>29.catch (ObjectNotFoundException) 
>30.{ 
>31.Console. WriteLine("Does not found on context ... "); 
>32.} 
>33.catch (NullReferenceException) 
>34.{ 
>35.Console.WriteLine("Somethings wrong with \\root\\cimv2"); 
>36.} } } } 
>37.static void Main(string[] args) 
>38.{ 
>39. . . .
>40.All_Driver_Existence.DeviceALL(); 
>41. . . . 
>42.} 
>43.}
错误代码第22行和第40行 堆栈跟踪:

不带调试的VisualStudio

>Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
>at System.Management.MTAHelper.IsNoContextMTA()
>at System.Management.MTAHelper.CreateInMTA(Type type)
>at System.Management.ManagementPath.CreateWbemPath(String path)
>at USBConsoleMonitor.All_Driver_Existence.Device_ALL() in >C:\Users\ComputerName\OneDrive\Desktop\Console Leader\USBConsoleMonitor\DeviceDriver.cs:line 25
>at USBConsoleMonitor.Runner.Main(String[] args) in >C:\Users\ComputerName\OneDrive\Desktop\Console Leader\USBConsoleMonitor\Runner.cs:line 27
动力壳

PS C:\Users\stran> System.Management.ManagementObjectSearcher("SELECT * FROM Win32_USBHub")
System.Management.ManagementObjectSearcher : The term 'System.Management.ManagementObjectSearcher' is not recognized
as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ System.Management.ManagementObjectSearcher("SELECT * FROM Win32_USBHu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.Manageme...tObjectSearcher:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

它是否与AMD GPU有关?我用笔记本电脑来编程。另外,我是一名硬件、软件开发人员。我渴望了解更多关于C和所有Visual Studio的信息。

我还使用Microsoft下载中的WMICODECREATOR->作为自动编程解决方案。代码不起作用。依赖项没有黄色状态,或者在错误列表中没有删除。我使用Windows Powershell查找解决方案时,它显示的内容与我的程序尝试访问的内容相同。相同的值。通过以下语法::System.Management.ManagementObjectSearcherSELECT*从Win32_usbhubi,您能否正确格式化代码并添加准确的错误消息和堆栈跟踪。我今天完成了对正文的规范修订。亚历克斯K