D
D
Denis2017-04-19 19:56:56
C++ / C#
Denis, 2017-04-19 19:56:56

How to fix "ManagementException not handled" error in VS2012?

I use the code to determine the temperature of the processor

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;    
using System.Management.Instrumentation; 

namespace temperature
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData WHERE Active=True");
            foreach (ManagementObject obj in searcher.Get())
            {
                byte[] vendorSpec = obj["VendorSpecific"] as byte[];
                if (vendorSpec != null)
                {
                    Console.WriteLine("Температура = " + vendorSpec[115]);
                }

            }

            Console.ReadKey();
        }
    }
}

At startup it gives a ManagementException not handled error , screenshot attached.
I must say right away that I connected References System.Management and System.Management.Instrumentation . Please help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew K., 2017-04-19
@svrap

Not enough access rights. Run the resulting exe as an administrator, or run the entire Visual Studio as an administrator. I recommend testing all wmi requests with wbemtest or powershell cmdlet Get-WmiObject

M
Maxim, 2017-04-19
@Got_Oxidus

Exception Handling

D
d-stream, 2017-04-19
@d-stream

class Program
    {
        static void Main(string[] args)
        {
try{
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData WHERE Active=True");
            foreach (ManagementObject obj in searcher.Get())
            {
                byte[] vendorSpec = obj["VendorSpecific"] as byte[];
                if (vendorSpec != null)
                {
                    Console.WriteLine("Температура = " + vendorSpec[115]);
                }

            }

            Console.ReadKey();
}
catch(Exception ex)      {
 ex.message - будет содержать текст сообщения об ошибке и его можно вывести через messagebox или хотя бы посмотреть в отладке
}
  }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question