Esempio n. 1
0
        private bool read()
        {
            ObjectQuery query;
            ManagementObjectSearcher searcher;

            ConnectionOptions connection = new ConnectionOptions();

            connection.Impersonation = ImpersonationLevel.Impersonate;
            ManagementScope scope = new ManagementScope("\\\\.\\Root\\OpenHardwareMonitor", connection);

            try
            {
                scope.Connect();
            }
            catch
            {
                return(false);
            }

            query    = new ObjectQuery("SELECT * FROM Hardware");
            searcher = new ManagementObjectSearcher(scope, query);

            //check if we have items
            int count = 0;

            try
            {
                count = searcher.Get().Count;
            }
            catch { }
            if (count == 0)
            {
                return(false);
            }

            List <OhmGroup> g_items = new List <OhmGroup>();

            foreach (ManagementObject queryObj in searcher.Get())
            {
                try
                {
                    OhmGroup group = new OhmGroup();
                    group.Name         = queryObj["Name"].ToString();
                    group.HardwareType = queryObj["HardwareType"].ToString();
                    group.Identifier   = queryObj["Identifier"].ToString();
                    g_items.Add(group);
                }
                catch { }
            }
            groups = g_items.ToArray();


            query    = new ObjectQuery("SELECT * FROM Sensor");
            searcher = new ManagementObjectSearcher(scope, query);

            //check if we have items
            count = 0;
            try
            {
                count = searcher.Get().Count;
            }
            catch { }
            if (count == 0)
            {
                return(false);
            }

            List <OhmSensor> items = new List <OhmSensor>();

            foreach (ManagementObject queryObj in searcher.Get())
            {
                try
                {
                    OhmSensor sensor = new OhmSensor();
                    sensor.Name       = queryObj["Name"].ToString();
                    sensor.Identifier = queryObj["Identifier"].ToString();
                    sensor.Parent     = queryObj["Parent"].ToString();
                    sensor.SensorType = queryObj["SensorType"].ToString();

                    object o_Value = queryObj["Value"];
                    if (o_Value != null && o_Value.GetType() == typeof(double))
                    {
                        sensor.Value = (double)o_Value;
                    }
                    else if (o_Value != null && o_Value.GetType() == typeof(Single))
                    {
                        sensor.Value = (Single)o_Value;
                    }
                    else if (o_Value != null && o_Value.GetType() == typeof(int))
                    {
                        sensor.Value = (int)o_Value;
                    }
                    else
                    {
                        sensor.Value = Double.NaN;
                    }

                    items.Add(sensor);
                }
                catch { }
            }
            sensors = items.ToArray();

            return(true);
        }
Esempio n. 2
0
        private bool read()
        {
            ObjectQuery query;
            ManagementObjectSearcher searcher;

            ConnectionOptions connection = new ConnectionOptions();
            connection.Impersonation = ImpersonationLevel.Impersonate;
            ManagementScope scope = new ManagementScope("\\\\.\\Root\\OpenHardwareMonitor", connection);
            try
            {
                scope.Connect();
            }
            catch
            {
                return false;
            }

            query = new ObjectQuery("SELECT * FROM Hardware");
            searcher = new ManagementObjectSearcher(scope, query);

            //check if we have items
            int count = 0;
            try
            {
                count = searcher.Get().Count;
            }
            catch { }
            if (count == 0)
                return false;

            List<OhmGroup> g_items = new List<OhmGroup>();
            foreach (ManagementObject queryObj in searcher.Get())
            {
                try
                {
                    OhmGroup group = new OhmGroup();
                    group.Name = queryObj["Name"].ToString();
                    group.HardwareType = queryObj["HardwareType"].ToString();
                    group.Identifier = queryObj["Identifier"].ToString();
                    g_items.Add(group);
                }
                catch { }
            }
            groups = g_items.ToArray();

            query = new ObjectQuery("SELECT * FROM Sensor");
            searcher = new ManagementObjectSearcher(scope, query);

            //check if we have items
            count = 0;
            try
            {
                count = searcher.Get().Count;
            }
            catch { }
            if (count == 0)
                return false;

            List<OhmSensor> items = new List<OhmSensor>();
            foreach (ManagementObject queryObj in searcher.Get())
            {
                try
                {
                    OhmSensor sensor = new OhmSensor();
                    sensor.Name = queryObj["Name"].ToString();
                    sensor.Identifier = queryObj["Identifier"].ToString();
                    sensor.Parent = queryObj["Parent"].ToString();
                    sensor.SensorType = queryObj["SensorType"].ToString();

                    object o_Value = queryObj["Value"];
                    if (o_Value != null && o_Value.GetType() == typeof(double))
                        sensor.Value = (double)o_Value;
                    else if (o_Value != null && o_Value.GetType() == typeof(Single))
                        sensor.Value = (Single)o_Value;
                    else if (o_Value != null && o_Value.GetType() == typeof(int))
                        sensor.Value = (int)o_Value;
                    else
                        sensor.Value = Double.NaN;

                    items.Add(sensor);
                }
                catch { }
            }
            sensors = items.ToArray();

            return true;
        }