aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SystemInfo/WMIReader.cs
blob: 2d8c95aae68081af339a522511d7210a703a7850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;

namespace Tango.SystemInfo
{
    class WMIReader
    {
        public static IList<SystemObject> GetPropertyValues(Connection WMIConnection,
                                                      string SelectQuery,
                                                      string className)
        {
            List<SystemObject> hardwareList = new List<SystemObject>();

            ManagementScope connectionScope = WMIConnection.GetConnectionScope;
            List<string> alProperties = new List<string>();
            SelectQuery msQuery = new SelectQuery(SelectQuery);
            ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(connectionScope, msQuery);

            try
            {
                foreach (ManagementObject item in searchProcedure.Get())
                {
                    SystemObject hardware = new SystemObject();

                    try
                    {
                        hardware.Name = item["Name"].ToString();
                    }
                    catch
                    {
                        hardware.Name = item.ToString();
                    }

                    hardwareList.Add(hardware);

                    foreach (string property in XMLConfig.GetSettings(className))
                    {
                        try
                        {
                            hardware.Properties.Add(new SystemObjectProperty()
                            {
                                Name = property,
                                Value = item[property].ToString()
                            });
                        }
                        catch (SystemException)
                        {
                            //Debug.WriteLine($"System Exception on {className}, {property}");
                        }
                    }
                }
            }
            catch (ManagementException e)
            {
                //Debug.WriteLine($"Management Exception on {className}");
            }

            return hardwareList;
        }
    }
}