|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Management; |
| 4 | +using System.Windows; |
| 5 | +using TheFlightSims.HyperVDPD.WMIProperties; |
| 6 | + |
| 7 | +namespace TheFlightSims.HyperVDPD |
| 8 | +{ |
| 9 | + public partial class AddDevice : Window |
| 10 | + { |
| 11 | + protected string deviceId; |
| 12 | + protected MachineMethods machine; |
| 13 | + |
| 14 | + public AddDevice(MachineMethods machine) |
| 15 | + { |
| 16 | + this.machine = machine; |
| 17 | + machine.Connect("root\\cimv2"); |
| 18 | + InitializeComponent(); |
| 19 | + } |
| 20 | + |
| 21 | + /* |
| 22 | + * Button and UI methods behaviour.... |
| 23 | + */ |
| 24 | + |
| 25 | + private void AddDeviceButton_Click(object sender, RoutedEventArgs e) |
| 26 | + { |
| 27 | + if (DeviceList.SelectedItem != null) |
| 28 | + { |
| 29 | + deviceId = DeviceList.SelectedItem.GetType().GetProperty("DeviceId").GetValue(DeviceList.SelectedItem, null).ToString(); |
| 30 | + DialogResult = true; |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + MessageBox.Show( |
| 35 | + "Please select a device to add.", |
| 36 | + "Warning", |
| 37 | + MessageBoxButton.OK, |
| 38 | + MessageBoxImage.Warning |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private void AddDeviceCloseButton_Click(object sender, RoutedEventArgs e) |
| 44 | + { |
| 45 | + Close(); |
| 46 | + } |
| 47 | + |
| 48 | + /* |
| 49 | + * Non-button methods |
| 50 | + */ |
| 51 | + |
| 52 | + public string GetDeviceId() |
| 53 | + { |
| 54 | + UpdateDevices(); |
| 55 | + ShowDialog(); |
| 56 | + |
| 57 | + return deviceId; |
| 58 | + } |
| 59 | + |
| 60 | + private void UpdateDevices() |
| 61 | + { |
| 62 | + DeviceList.Items.Clear(); |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + foreach (ManagementObject device in machine.GetObjects("Win32_PnPEntity", "Status, PNPClass, Name, DeviceID").Cast<ManagementObject>()) |
| 67 | + { |
| 68 | + string deviceStatus = device["Status"]?.ToString() ?? "Unknown"; |
| 69 | + string deviceType = device["PNPClass"]?.ToString() ?? "Unknown"; |
| 70 | + string deviceName = device["Name"]?.ToString() ?? "Unknown"; |
| 71 | + string deviceId = device["DeviceID"]?.ToString() ?? "Unknown"; |
| 72 | + |
| 73 | + if (!deviceId.StartsWith("PCI")) |
| 74 | + { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + DeviceList.Items.Add(new |
| 79 | + { |
| 80 | + DeviceStatus = deviceStatus, |
| 81 | + DeviceType = deviceType, |
| 82 | + DeviceName = deviceName, |
| 83 | + DeviceId = deviceId |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + catch (Exception ex) |
| 88 | + { |
| 89 | + WMIDefaultValues.HandleException(ex, machine.GetComputerName()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments