Exemplo n.º 1
0
 static string GetFromWmi(string query, string column, string scope = null)
 {
     try
     {
         IEnumerator wmi = new ManagementObjectSearcher(scope, query).Get().GetEnumerator();
         return wmi.MoveNext() ? ((ManagementObject)wmi.Current)[column].ToString() : null;
     }
     catch
     {
         return null;
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// get parent pid for specified process
 /// </summary>
 /// <param name="cpid">child process</param>
 /// <returns>uint</returns>
 static uint getParentPid(int cpid)
 {
     string query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", cpid);
     var results = new ManagementObjectSearcher("root\\CIMV2", query).Get().GetEnumerator();
     results.MoveNext();
     ManagementBaseObject queryObj = results.Current;
     return (uint)queryObj["ParentProcessId"];
 }
Exemplo n.º 3
0
        public List<DriveInformation> GetDriveInformation()
        {
            var q = new WqlObjectQuery("SELECT * FROM Win32_DiskDrive");
            var res = new ManagementObjectSearcher(q);
            var driveNames = (from ManagementBaseObject o in res.Get() select o["Model"]?.ToString()).ToList();
            var driveList = new List<DriveInformation>();
            var drives = DriveInfo.GetDrives();
            for (var index = 0; index < drives.Length; index++)
            {
                try
                {
                    var drive = drives[index];
                    var driveInfo = new DriveInformation();
                    if (!drive.IsReady) continue;
                    driveInfo.Model = driveNames.ElementAtOrDefault(index) != null ? driveNames[index] : "Unknown Model";
                    driveInfo.Name = drive.Name;
                    driveInfo.FreeSpace = drive.TotalFreeSpace;
                    driveInfo.TotalSize = drive.TotalSize;
                    driveInfo.DriveType = drive.DriveType.ToString();
                    driveInfo.DriveFormat = drive.DriveFormat;
                    driveInfo.VolumeLabel = drive.VolumeLabel;
                    driveInfo.RootDirectory = drive.RootDirectory.ToString();
                    driveInfo.IsReady = drive.IsReady;
                    //just set it for now
                    driveInfo.SmartData = new List<SmartModel>();
                    driveInfo.Partitions = new List<PartitionModel>();
                    driveList.Add(driveInfo);
                    try
                    {
                        var mosDisks =
                                       new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" +
                                                                    driveInfo.Model + "'").Get().GetEnumerator();
                        if (!mosDisks.MoveNext()) continue;
                        driveInfo.MediaType = mosDisks.Current.GetPropertyValue("MediaType")?.ToString() ?? "Unknown";
                        driveInfo.Serial = mosDisks.Current.GetPropertyValue("SerialNumber")?.ToString()?.Trim() ?? "Unknown";
                        driveInfo.Interface = mosDisks.Current.GetPropertyValue("InterfaceType")?.ToString() ?? "Unknown";
                        driveInfo.TotalPartitions = mosDisks.Current.GetPropertyValue("Partitions")?.ToString() ?? "Unknown";
                        driveInfo.Signature = mosDisks.Current.GetPropertyValue("Signature")?.ToString() ?? "Unknown";
                        driveInfo.Firmware = mosDisks.Current.GetPropertyValue("FirmwareRevision")?.ToString() ?? "Unknown";
                        driveInfo.Cylinders = mosDisks.Current.GetPropertyValue("TotalCylinders")?.ToString() ?? "Unknown";
                        driveInfo.Sectors = mosDisks.Current.GetPropertyValue("TotalSectors")?.ToString() ?? "Unknown";
                        driveInfo.Heads = mosDisks.Current.GetPropertyValue("TotalHeads")?.ToString() ?? "Unknown";
                        driveInfo.Tracks = mosDisks.Current.GetPropertyValue("TotalTracks")?.ToString() ?? "Unknown";
                        driveInfo.BytesPerSecond = mosDisks.Current.GetPropertyValue("BytesPerSector")?.ToString() ?? "Unknown";
                        driveInfo.SectorsPerTrack = mosDisks.Current.GetPropertyValue("SectorsPerTrack")?.ToString() ?? "Unknown";
                        driveInfo.TracksPerCylinder = mosDisks.Current.GetPropertyValue("TracksPerCylinder")?.ToString() ?? "Unknown";
                    }
                    catch (Exception)
                    {

                       //fail
                    }
                    
                    try
                    {
                        var mosPartition =
                                     new ManagementObjectSearcher("SELECT * FROM Win32_DiskPartition WHERE DiskIndex = '" +
                                                                  index + "'").Get().GetEnumerator();
                        while (mosPartition.MoveNext())
                        {
                            var partion = new PartitionModel
                            {
                                Name = mosPartition.Current.GetPropertyValue("Name")?.ToString() ?? "Unknown",
                                Size = mosPartition.Current.GetPropertyValue("Size")?.ToString() ?? "Unknown",
                                BlockSize = mosPartition.Current.GetPropertyValue("BlockSize")?.ToString() ?? "Unknown",
                                StartingOffset =
                                    mosPartition.Current.GetPropertyValue("StartingOffset")?.ToString() ?? "Unknown",
                                Index = mosPartition.Current.GetPropertyValue("Index")?.ToString() ?? "Unknown",
                                DiskIndex = mosPartition.Current.GetPropertyValue("DiskIndex")?.ToString() ?? "Unknown",
                                BootPartition = mosPartition.Current.GetPropertyValue("BootPartition")?.ToString() ?? "Unknown",
                                PrimaryPartition =
                                    mosPartition.Current.GetPropertyValue("PrimaryPartition")?.ToString() ?? "Unknown",
                                Bootable = mosPartition.Current.GetPropertyValue("Bootable")?.ToString() ?? "Unknown"
                            };
                            driveInfo.Partitions.Add(partion);
                        }
                    }
                    catch (Exception)
                    {

                        //fail
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
            }

            using (var searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData"))
            using (var thresSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_FailurePredictThresholds"))
            using (var failureSearch = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_FailurePredictStatus"))
            {


                try
                {
                    var searcherEnumerator = searcher.Get().GetEnumerator();
                    var thresSearcherEnumerator = thresSearcher.Get().GetEnumerator();
                    var failureSearchEnumerator = failureSearch.Get().GetEnumerator();

                    var index = 0;
                    while (searcherEnumerator.MoveNext() && thresSearcherEnumerator.MoveNext())
                    {
                        var arrVendorSpecific = (byte[])searcherEnumerator.Current.GetPropertyValue("VendorSpecific");
                        var arrThreshold = (byte[])thresSearcherEnumerator.Current.GetPropertyValue("VendorSpecific");

                        /* Create SMART data from 'vendor specific' array */
                        var d = new SmartData(arrVendorSpecific, arrThreshold);
                        var smartRows = (from b in d.Attributes where !Regex.IsMatch(b.AttributeType.ToString(), @"^\d+$") let rawData = BitConverter.ToString(b.VendorData.Reverse().ToArray()).Replace("-", string.Empty) select new SmartModel(b.AttributeType.ToString(), b.Value.ToString(CultureInfo.InvariantCulture), b.Threshold.ToString(CultureInfo.InvariantCulture), rawData, long.Parse(rawData, NumberStyles.HexNumber).ToString(CultureInfo.InvariantCulture))).ToList();
                        driveList.ElementAt(index).SmartData = smartRows;
                        if (failureSearchEnumerator.MoveNext())
                        {
                            driveList.ElementAt(index).DriveHealth = (bool)failureSearchEnumerator.Current.GetPropertyValue("PredictFailure") ? "WARNING" : "OK";
                        }
                        index++;
                    }
                }
                catch (Exception)
                {

                    //if we fail, do nothing more.
                }
            }
            return driveList;
        }
Exemplo n.º 4
0
 private static int GetPhisicalMemory()
 {
     ManagementObjectCollection.ManagementObjectEnumerator enumerator = new ManagementObjectSearcher { Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" }) }.Get().GetEnumerator();
     int num = 0;
     while (enumerator.MoveNext())
     {
         ManagementBaseObject current = enumerator.Current;
         if (current.Properties["Capacity"].Value != null)
         {
             try
             {
                 num += int.Parse(current.Properties["Capacity"].Value.ToString());
             }
             catch
             {
                 return 0;
             }
         }
     }
     return ((num / 0x400) / 0x400);
 }