WMI 类异常“无效类”

WMI class exception "Invalid class"

提问人:Abhijeet Kumar Jha 提问时间:10/31/2023 最后编辑:Ted LyngmoAbhijeet Kumar Jha 更新时间:10/31/2023 访问量:34

问:

我正在尝试使用以下代码使用 WMI 或其他方式在 .NET 中获取 SMART 寄存器,这会导致以下数据,但我在所有代码中都收到“无效类”异常。

注册名称 当前值 最差值 阈值 数据(供应商) SMART 状态

enter code here

代码 1


     try
       {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"SELECT * 
                                            FROM MSStorageDriver_ATAPISMARTData");

       foreach (ManagementObject disk in searcher.Get())
        {
            // Retrieve SMART attributes
            ushort[] rawValues = (ushort[])disk["VendorSpecific"];

            // Extract specific SMART attributes
            string registerName = Encoding.Default.GetString(rawValues, 2, 2);  
            ushort currentValue = rawValues[0];  // Current Value
            ushort worstValue = rawValues[1];    // Worst Value
            ushort thresholdValue = rawValues[3]; // Threshold Value

            Console.WriteLine($"Register Name: {registerName}");
            Console.WriteLine($"Current Value: {currentValue}");
            Console.WriteLine($"Worst Value: {worstValue}");
            Console.WriteLine($"Threshold Value: {thresholdValue}");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: " + ex.Message);
    }

在此输入验证码 代码 2


      try
      {
          var searcher = new ManagementObjectSearcher("Select * from 
                           Win32_DiskDrive");
           searcher.Scope = new ManagementScope(@"\root\wmi");      
           searcher.Query = new ObjectQuery("Select * from 
                             MSStorageDriver_FailurePredictStatus");          
      
           foreach (ManagementObject drive in searcher.Get())
            {
              --
            }
      }

enter code here

代码 3


 try 
   {
      ManagementScope scope =  new ManagementScope( "\\\\FullComputerName\\root\\cimv2");
      scope.Connect();
      ObjectQuery query = new ObjectQuery("Select * from  MSStorageDriver_FailurePredictData");
      ManagementObjectSearcher searcher =new ManagementObjectSearcher(scope,query);

      foreach (ManagementObject drive in searcher.Get())
        {
          --
        }
    }
C# .NET Windows WMI

评论

0赞 Alander 10/31/2023
这回答了你的问题吗?如何在 C# .NET 中读取 HDD S.M.A.R.T 寄存器并获取该寄存器的所有关联值?
0赞 Abhijeet Kumar Jha 10/31/2023
对于上述所有代码,我都收到无效类异常
1赞 Hans Passant 10/31/2023
您必须选择命名空间,使用 new ManagementObjectSearcher(“root\\WMI”, “SELECT * FROM MSStorageDriver_ATAPISmartData”);WMI Code Creator 实用程序可以方便地完成此操作。不要在带有 SSD 的机器上尝试此操作。
0赞 Abhijeet Kumar Jha 10/31/2023
我只能尝试使用 SSD

答: 暂无答案