/// <summary> /// Retrieves the MAC addresses using the (old) Win32_NetworkAdapter WMI class. /// </summary> /// <returns>A list of MAC addresses.</returns> internal List <string> GetMacAddressesUsingWmi() { var values = new List <string>(); var adapters = WmiHelper.GetWMIInstances(@"root\cimv2", "Win32_NetworkAdapter", new string[] { "MACAddress", "PhysicalAdapter" }); foreach (var adapter in adapters) { try { var isPhysical = Boolean.Parse(adapter["PhysicalAdapter"] as string); if (_excludeNonPhysical && !isPhysical) { continue; } var macAddress = adapter["MACAddress"] as string; if (!string.IsNullOrEmpty(macAddress)) { values.Add(macAddress); } } catch { } } return(values); }
/// <summary> /// Gets the component value. /// </summary> /// <returns>The component value.</returns> public string GetValue() { try { var systemLogicalDiskDeviceId = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2); var logicalDiskToPartition = Array.Find(WmiHelper.GetWMIInstances(@"root\cimv2", "Win32_LogicalDiskToPartition"), logicalDriveToPartition => (logicalDriveToPartition["Dependent"] as IDictionary <string, object>)["DeviceID"] as string == systemLogicalDiskDeviceId); var partition = Array.Find(WmiHelper.GetWMIInstances(@"root\cimv2", "Win32_DiskDriveToDiskPartition"), partition => (partition["Dependent"] as IDictionary <string, object>)["DeviceID"] as string == (logicalDiskToPartition["Antecedent"] as IDictionary <string, object>)["DeviceID"] as string); var diskdrive = Array.Find(WmiHelper.GetWMIInstances(@"root\cimv2", "Win32_DiskDrive "), diskDriveToPartition => diskDriveToPartition["DeviceID"] as string == (partition["Antecedent"] as IDictionary <string, object>)["DeviceID"] as string); return(diskdrive["SerialNumber"] as string); } catch (Exception e) { throw new DeviceIdComponentFailedToObtainValueException("Failed to GetValue() in SystemDriveSerialNumberDeviceIdComponent", e); } }
/// <summary> /// Retrieves the MAC addresses using the CIMv2 based MSFT_NetAdapter interface (Windows 8 and up). /// </summary> /// <returns>A list of MAC addresses.</returns> internal List <string> GetMacAddressesUsingCimV2() { var values = new List <string>(); var adapters = WmiHelper.GetWMIInstances(@"root\StandardCimv2", "MSFT_NetAdapter", new string[] { "ConnectorPresent", "NdisPhysicalMedium", "PermanentAddress" }); foreach (var adapter in adapters) { try { var isPhysical = Boolean.Parse(adapter["ConnectorPresent"] as string); var ndisMedium = UInt32.Parse(adapter["NdisPhysicalMedium"] as string); // Skip non physcial adapters if instructed to do so. if (_excludeNonPhysical && !isPhysical) { continue; } // Skip wireless adapters if instructed to do so. if (_excludeWireless && ndisMedium == 9) // Native802_11 { continue; } // Add the MAC address to the list of values. var value = adapter["PermanentAddress"] as string; if (value != null) { // Ensure the hardware addresses are formatted as MAC addresses if possible. // This is a discrepancy between the MSFT_NetAdapter and Win32_NetworkAdapter interfaces. value = FormatMacAddress(value); values.Add(value); } } catch { } } return(values); }
/// <summary> /// Gets the component value. /// </summary> /// <returns>The component value.</returns> public string GetValue() { var values = new List <string>(); foreach (var obj in WmiHelper.GetWMIInstances(@"root\cimv2", _wmiClass)) { try { values.Add(((IDictionary <string, object>)obj)[_wmiProperty].ToString()); } catch { } } values.Sort(); return((values != null && values.Count > 0) ? string.Join(",", values.ToArray()) : null); }