예제 #1
0
        public static bool TrySelect(
            string wql,
            string[] fieldsByPriority,
            out DataTable objects,
            out Exception failOfGet)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            DataTable dataTable = new DataTable("WMI");
            int       rowIndex  = 0;

            using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wql))
            {
                try
                {
                    foreach (ManagementBaseObject wmiRow in managementObjectSearcher.Get())
                    {
                        DataRow row = dataTable.NewRow();
                        if (rowIndex == 0)
                        {
                            // Properties of the First Row.
                            string[] propertyNames = new string[wmiRow.Properties.Count];
                            int      propertyIndex = 0;
                            foreach (PropertyData property in wmiRow.Properties)
                            {
                                propertyNames[propertyIndex++] = property.Name;
                            }

                            WmiUtils.ComparerByPriority comparerByPriority = new WmiUtils.ComparerByPriority(fieldsByPriority ?? new string[0], propertyNames);
                            Array.Sort(propertyNames, comparerByPriority);
                            foreach (string columnName in propertyNames)
                            {
                                dataTable.Columns.Add(columnName, typeof(object));
                            }
                        }

                        foreach (DataColumn column in dataTable.Columns)
                        {
                            object propertyValue;
                            if (WmiUtils.TryGetProperty(wmiRow, column.ColumnName, out propertyValue))
                            {
                                row[column.ColumnName] = propertyValue;
                                object copy = row[column.ColumnName];
                            }
                        }
                        dataTable.Rows.Add(row);
                        ++rowIndex;
                    }
                }
                catch (Exception ex)
                {
                    objects   = (DataTable)null;
                    failOfGet = ex;
                    return(false);
                }
            }
            Trace.WriteLine("Scan WMI list '" + wql + "' by " + stopwatch.ElapsedMilliseconds.ToString("n0") + " msec");
            objects   = dataTable;
            failOfGet = (Exception)null;
            return(true);
        }
예제 #2
0
        internal static dynamic[] FetchFromQuery(string wql, string[] fieldsByPriority = null)
        {
            List <dynamic> ret = new List <dynamic>();

            Stopwatch stopwatch = Stopwatch.StartNew();
            DataTable dataTable = new DataTable("WMI");
            int       rowIndex  = 0;

            using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wql))
            {
                string[] propertyNames = null;
                foreach (ManagementBaseObject wmiRow in managementObjectSearcher.Get())
                {
                    dynamic row = new ExpandoObject();
                    if (rowIndex == 0)
                    {
                        // Properties of the First Row.
                        propertyNames = new string[wmiRow.Properties.Count];
                        int propertyIndex = 0;
                        foreach (PropertyData property in wmiRow.Properties)
                        {
                            propertyNames[propertyIndex++] = property.Name;
                        }

                        ComparerByPriority comparerByPriority = new WmiUtils.ComparerByPriority(fieldsByPriority ?? new string[0], propertyNames);
                        Array.Sort(propertyNames, comparerByPriority);
                    }

                    foreach (string propertyName in propertyNames)
                    {
                        object propertyValue;
                        if (TryGetProperty(wmiRow, propertyName, out propertyValue))
                        {
                            ((IDictionary <string, Object>)row)[propertyName] = propertyValue;
                        }
                    }

                    ret.Add(row);
                    ++rowIndex;
                }
            }

            Debug.WriteLine("Scan WMI list '" + wql + "' by " + stopwatch.ElapsedMilliseconds.ToString("n0") + " msec");
            return(ret.ToArray());
        }