-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddDevice.xaml.cs
More file actions
152 lines (134 loc) · 5.17 KB
/
AddDevice.xaml.cs
File metadata and controls
152 lines (134 loc) · 5.17 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Linq;
using System.Management;
using System.Windows;
using TheFlightSims.HyperVDDA.DefaultUI;
using TheFlightSims.HyperVDDA.WMIProperties;
/*
* Primary namespace for HyperV-DDA application
* It contains the main window and all related methods for the core application
*/
namespace TheFlightSims.HyperVDDA
{
/*
* Add Device Window class
* It is used to add device into the selected VM
*/
public partial class AddDevice : Window
{
////////////////////////////////////////////////////////////////
/// Global Properties and Constructors Region
/// This region contains global properties and constructors
/// for the MachineMethods class.
////////////////////////////////////////////////////////////////
/*
* Global properties
* deviceId: Device ID of the selected device
* machine: WMI namespace of the machine
*/
protected string deviceId;
protected MachineMethods machine;
/*
* Constructor of the MainWindow class
* Initializes the components and sets up event handlers
*/
public AddDevice(MachineMethods machine)
{
this.machine = machine;
machine.Connect("root\\cimv2");
InitializeComponent();
}
////////////////////////////////////////////////////////////////
/// User Action Methods Region
/// This region contains methods that handle user actions.
/// For example, button clicks, changes in order.
////////////////////////////////////////////////////////////////
/*
* Actions for button "Add Device"
*/
private void AddDeviceButton_Click(object sender, RoutedEventArgs e)
{
// If the selected item is not null (nothing is selected), proceed
if (DeviceList.SelectedItem != null)
{
deviceId = DeviceList.SelectedItem.GetType().GetProperty("DeviceId").GetValue(DeviceList.SelectedItem, null).ToString();
DialogResult = true;
}
else
{
// Notify user if nothing is selected
_ = MessageBox.Show(
"Please select a device to add.",
"Warning",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
}
}
/*
* Actions for button "Close"
*/
private void AddDeviceCloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
////////////////////////////////////////////////////////////////
/// Non-User Action Methods Region
///
/// This region contains methods that do not handle user actions.
///
/// Think about this is the back-end section.
/// It should not be in a seperated class, because it directly interacts with the UI elements.
////////////////////////////////////////////////////////////////
/*
* Start getting the device ID
*/
public string GetDeviceId()
{
UpdateDevices();
_ = ShowDialog();
// Return the device ID back to the main windows
return deviceId;
}
/*
* Update device list
*/
private async void UpdateDevices()
{
// Clear the device list
DeviceList.Items.Clear();
try
{
// Loop for devices in Win32_PnPEntity
using (ManagementObjectCollection pnpEntityList = machine.GetObjects("Win32_PnPEntity", "Status, PNPClass, Name, DeviceID"))
{
foreach (ManagementObject device in pnpEntityList.Cast<ManagementObject>())
{
string deviceStatus = device["Status"]?.ToString() ?? "Unknown";
string deviceType = device["PNPClass"]?.ToString() ?? "Unknown";
string deviceName = device["Name"]?.ToString() ?? "Unknown";
string deviceId = device["DeviceID"]?.ToString() ?? "Unknown";
// If the device is not PCI, skip to the next device
if (!deviceId.StartsWith("PCI"))
{
device.Dispose();
continue;
}
_ = DeviceList.Items.Add(new
{
DeviceStatus = deviceStatus,
DeviceType = deviceType,
DeviceName = deviceName,
DeviceId = deviceId
});
device.Dispose();
}
}
}
catch (Exception ex)
{
(new ExceptionView()).HandleException(ex);
}
}
}
}