-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
293 lines (264 loc) · 11.6 KB
/
MainWindow.xaml.cs
File metadata and controls
293 lines (264 loc) · 11.6 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using GetWMIBasic.DefaultUI;
using GetWMIBasic.WMIMethods;
using System;
using System.Linq;
using System.Management;
using System.Security;
using System.Threading.Tasks;
using System.Windows;
/*
* Primary namespace for the application
* It includes the MainWindow class which is the main UI window of the application
* and additional UI for each featuring
*/
namespace GetWMIBasic
{
/*
* Main Window class
* It is used to display the main UI of the application
*/
public partial class MainWindow : Window
{
////////////////////////////////////////////////////////////////
/// Global Properties and Constructors Region
/// This region contains global properties and constructors
/// for the MachineMethods class.
////////////////////////////////////////////////////////////////
/*
* Global properties
* machine: An instance of the MachineMethods class to perform WMI operations
*/
protected MachineMethods machine;
/*
* Constructor of the MainWindow class
* Initializes the components and sets up event handlers
*/
public MainWindow()
{
// Initialize components
InitializeComponent();
// Initialize the MachineMethods instance for local machine
machine = new MachineMethods();
// Set up the sync loading event handler. See the method below for further info.
Loaded += MainWindow_Loaded;
}
/*
* Main Windows async loading method.
* It is used prevent long loading times on the UI thread
*/
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Call the Refresh method to load initial data
await Refresh();
}
////////////////////////////////////////////////////////////////
/// User Action Methods Region
/// This region contains methods that handle user actions.
/// For example, button clicks, changes in order.
////////////////////////////////////////////////////////////////
/*
* Actions for button "Connect to Another Computer"
*/
private async void ConnectToAnotherComputer_Button(object sender, RoutedEventArgs e)
{
// Get the user credentials from the ConnectForm
(string computerName, string username, SecureString password) userCredential = (new ConnectForm()).ReturnValue();
// If the computer name is not empty, create a new MachineMethods instance
if (userCredential.computerName != "")
{
machine = !userCredential.computerName.Equals("localhost") ? new MachineMethods(userCredential) : new MachineMethods();
}
// Refresh the data on the UI
await Refresh();
}
/*
* Actons for button "Exit Application"
*/
private void Exit_Button(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
/*
*
*/
private async void Restart_Button(object sender, RoutedEventArgs e)
{
// Confirm the restart action with the user
if (MessageBox.Show($"Do you want to restart the computer {machine.GetComputerName()}?", "Proceed?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// Update the status bar to indicate loading
StatusBarChange("Loading", true);
/*
* Try to restart the remote machine using WMI, with the target computer name
* Catch the exception and display it using the ExceptionView if any error occurs
* In any case, no matter if it succeeds or fails, update the status bar to "Done"
*/
try
{
// Since the local machine cannot be restarted remotely, throw an exception if the target is localhost
// It should be done via the user context menu instead
if (machine.GetComputerName() == "localhost")
{
throw new ManagementException("Local Host cannot be restarted");
}
// Connect to the WMI namespace
machine.Connect("root\\cimv2");
// Call the Win32Shutdown method with the restart flag (6)
await machine.CallMethod("Win32_OperatingSystem", "*", "Win32Shutdown", new object[] { 6 });
}
catch (Exception ex)
{
// Handle any exceptions using the ExceptionView
(new ExceptionView()).HandleException(ex);
}
finally
{
// Update the status bar to indicate completion
StatusBarChange("Done", false);
}
}
}
/*
* Actions for button "Shutdown Computer"
*/
private async void Shutdown_Button(object sender, RoutedEventArgs e)
{
// Confirm the shutdown action with the user
if (MessageBox.Show($"Do you want to shutdown the computer {machine.GetComputerName()}?", "Proceed?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// Update the status bar to indicate loading
StatusBarChange("Loading", true);
/*
* Try to shutdown the remote machine using WMI, with the target computer name
* Catch the exception and display it using the ExceptionView if any error occurs
* In any case, no matter if it succeeds or fails, update the status bar to "Done"
*/
try
{
// Since the local machine cannot be shutdown remotely, throw an exception if the target is localhost
// It should be done via the user context menu instead
if (machine.GetComputerName() == "localhost")
{
throw new ManagementException("Local Host cannot be shut down");
}
// Connect to the WMI namespace
machine.Connect("root\\cimv2");
// Call the Win32Shutdown method with the shutdown flag (5)
await machine.CallMethod("Win32_OperatingSystem", "*", "Win32Shutdown", new object[] { 5 });
}
catch (Exception ex)
{
// Handle any exceptions using the ExceptionView
(new ExceptionView()).HandleException(ex);
}
finally
{
// Update the status bar to indicate completion
StatusBarChange("Done", false);
}
}
}
/*
* Actions for button "Refresh Information"
*/
private async void Refresh_Button(object sender, RoutedEventArgs e)
{
// Call the Refresh method to update the data on the UI
await Refresh();
}
/*
* Actions for button "Regular Exception Example"
* (It will throw and catch a common exception to demonstrate the ExceptionView)
*/
private void RegularException_Click(object sender, RoutedEventArgs e)
{
try
{
throw new ManagementException("Unable to manage (example exception handler).");
}
catch (Exception ex)
{
(new ExceptionView()).HandleException(ex);
}
}
/*
* Actions for button "Critical Exception Example"
* (It will throw and catch a critical exception to demonstrate the ExceptionView)
*/
private void CriticalException_Click(object sender, RoutedEventArgs e)
{
try
{
throw new Exception("Critical exception (example exception handler).");
}
catch (Exception ex)
{
(new ExceptionView()).HandleException(ex);
}
}
////////////////////////////////////////////////////////////////
/// 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.
////////////////////////////////////////////////////////////////
/*
* Refresh method to update the information displayed on the UI
*/
private async Task Refresh()
{
// Update the status bar to indicate loading
StatusBarChange("Loading", true);
/*
* Try to get info from the remote machine using WMI, with the target computer name
* Catch the exception and display it using the ExceptionView if any error occurs
* In any case, no matter if it succeeds or fails, update the status bar to "Done"
*/
try
{
// Connect to the WMI namespace
machine.Connect("root\\cimv2");
// Get BIOS properties
// In case the property is null, set the text to an empty string
Task<ManagementObjectCollection> biosProperties = machine.GetObjects("Win32_BIOS", "Manufacturer, Name, SerialNumber, Version");
foreach (ManagementObject biosProperty in (await biosProperties).Cast<ManagementObject>())
{
BIOS_Manufacturer.Text = biosProperty["Manufacturer"]?.ToString() ?? string.Empty;
BIOS_Name.Text = biosProperty["Name"]?.ToString() ?? string.Empty;
BIOS_SN.Text = biosProperty["SerialNumber"]?.ToString() ?? string.Empty;
BIOS_Version.Text = biosProperty["Version"]?.ToString() ?? string.Empty;
}
// Get Operating System properties
// In case the property is null, set the text to an empty string
Task<ManagementObjectCollection> osProperties = machine.GetObjects("Win32_ComputerSystem", "Name, Domain, TotalPhysicalMemory, SystemType");
foreach (ManagementObject osProperty in (await osProperties).Cast<ManagementObject>())
{
Computer_Name.Text = osProperty["Name"]?.ToString() ?? string.Empty;
Computer_Domain.Text = osProperty["Domain"]?.ToString() ?? string.Empty;
Computer_Memory.Text = osProperty["TotalPhysicalMemory"]?.ToString() ?? string.Empty;
Computer_SysType.Text = osProperty["SystemType"]?.ToString() ?? string.Empty;
}
}
catch (Exception ex)
{
// Handle any exceptions using the ExceptionView
(new ExceptionView()).HandleException(ex);
}
finally
{
// Update the status bar to indicate completion
StatusBarChange("Done", false);
}
}
/*
* Set the status bar text and progress bar state
*/
protected void StatusBarChange(string label, bool progressbarLoading)
{
Bottom_Label.Text = label;
Bottom_ProgressBar.IsIndeterminate = progressbarLoading;
}
}
}