Skip to content

Commit 75ee12e

Browse files
update for correctness
Co-Authored-By: VLTA of @TheFlightSims <176574466+anhvlttfs@users.noreply.github.com>
1 parent dff70fa commit 75ee12e

8 files changed

Lines changed: 57 additions & 139 deletions

File tree

DefaultUI/ConnectForm.xaml

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
xmlns:local="clr-namespace:GetWMIBasic"
77
mc:Ignorable="d"
88
Title="Connect To Another Computer"
9-
Height="160"
10-
Width="400"
9+
Height="180"
10+
Width="400"
11+
MinHeight="180"
12+
MinWidth="400"
13+
MaxHeight="180"
14+
MaxWidth="400"
1115
ResizeMode="NoResize"
1216
SizeToContent="WidthAndHeight">
1317
<Grid>
1418
<Grid.RowDefinitions>
15-
<RowDefinition/>
16-
<RowDefinition/>
17-
<RowDefinition/>
18-
<RowDefinition/>
19-
<RowDefinition/>
19+
<RowDefinition Height="*"/>
20+
<RowDefinition Height="3*"/>
21+
<RowDefinition Height="*"/>
2022
</Grid.RowDefinitions>
2123
<Grid Grid.Row="0">
2224
<Grid.Margin>
@@ -32,34 +34,21 @@
3234
<ColumnDefinition Width="2*"/>
3335
<ColumnDefinition Width="5*"/>
3436
</Grid.ColumnDefinitions>
35-
<Label Grid.Column="0" VerticalAlignment="Center">Computer Name</Label>
36-
<TextBox x:Name="ComputerName_TextBox" Grid.Column="1" VerticalAlignment="Center"/>
37+
<Grid.RowDefinitions>
38+
<RowDefinition/>
39+
<RowDefinition/>
40+
<RowDefinition/>
41+
</Grid.RowDefinitions>
42+
<Label Grid.Row="0" Grid.Column="0" VerticalAlignment="Center">Computer Name</Label>
43+
<TextBox Grid.Row="0" Grid.Column="1" x:Name="ComputerName_TextBox" VerticalAlignment="Center"/>
44+
<Label Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">User Name</Label>
45+
<TextBox Grid.Row="1" Grid.Column="1" x:Name="UserName_TextBox" VerticalAlignment="Center"/>
46+
<Label Grid.Row="2" Grid.Column="0" VerticalAlignment="Center">Password</Label>
47+
<PasswordBox Grid.Row="2" Grid.Column="1" x:Name="Password_TextBox" VerticalAlignment="Center"/>
3748
</Grid>
3849
<Grid Grid.Row="2">
3950
<Grid.Margin>
40-
<Thickness Left="5" Right="5"></Thickness>
41-
</Grid.Margin>
42-
<Grid.ColumnDefinitions>
43-
<ColumnDefinition Width="2*"/>
44-
<ColumnDefinition Width="5*"/>
45-
</Grid.ColumnDefinitions>
46-
<Label Grid.Column="0" VerticalAlignment="Center">User Name</Label>
47-
<TextBox x:Name="UserName_TextBox" Grid.Column="1" VerticalAlignment="Center"/>
48-
</Grid>
49-
<Grid Grid.Row="3">
50-
<Grid.Margin>
51-
<Thickness Left="5" Right="5"></Thickness>
52-
</Grid.Margin>
53-
<Grid.ColumnDefinitions>
54-
<ColumnDefinition Width="2*"/>
55-
<ColumnDefinition Width="5*"/>
56-
</Grid.ColumnDefinitions>
57-
<Label Grid.Column="0" VerticalAlignment="Center">Password</Label>
58-
<PasswordBox x:Name="Password_TextBox" Grid.Column="1" VerticalAlignment="Center"/>
59-
</Grid>
60-
<Grid Grid.Row="4">
61-
<Grid.Margin>
62-
<Thickness Left="5" Right="5"></Thickness>
51+
<Thickness Left="5" Right="5" Bottom="5"></Thickness>
6352
</Grid.Margin>
6453
<Grid.ColumnDefinitions>
6554
<ColumnDefinition Width="*"/>

DefaultUI/ExceptionView.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public partial class ExceptionView : Window
3131
{
3232
typeof(UnauthorizedAccessException),
3333
typeof(COMException),
34-
typeof(ManagementException),
35-
typeof(CommonException)
34+
typeof(ManagementException)
3635
};
3736

3837
// Constructor of the Exception View class

Get WMI Basic.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@
102102
<Compile Include="DefaultUI\ExceptionView.xaml.cs">
103103
<DependentUpon>ExceptionView.xaml</DependentUpon>
104104
</Compile>
105-
<Compile Include="WMIMethods\CommonException.cs" />
106-
<Compile Include="WMIMethods\CriticalException.cs" />
107105
<Compile Include="WMIMethods\MachineMethods.cs" />
108106
<Page Include="DefaultUI\ConnectForm.xaml">
109107
<SubType>Designer</SubType>

MainWindow.xaml

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
xmlns:local="clr-namespace:GetWMIBasic"
77
mc:Ignorable="d"
88
Title="Get WMI Basic"
9-
Height="192"
9+
Height="200"
1010
Width="800"
11+
MinWidth="600"
12+
MaxWidth="1000"
1113
ResizeMode="NoResize"
1214
SizeToContent="WidthAndHeight">
1315
<Grid>
1416
<Grid.RowDefinitions>
15-
<RowDefinition Height="20"/>
16-
<RowDefinition Height="130"/>
17-
<RowDefinition/>
17+
<RowDefinition Height="*"/>
18+
<RowDefinition Height="5*"/>
19+
<RowDefinition Height="*"/>
1820
</Grid.RowDefinitions>
1921
<Grid Grid.Row="0">
2022
<Menu IsMainMenu="True" >
@@ -37,18 +39,18 @@
3739
</Grid>
3840
<Grid Grid.Row="1">
3941
<Grid.Margin>
40-
<Thickness Left="10" Right="10"></Thickness>
42+
<Thickness Left="5" Right="5"></Thickness>
4143
</Grid.Margin>
4244
<Grid.ColumnDefinitions>
43-
<ColumnDefinition Width="390" />
44-
<ColumnDefinition Width="390" />
45+
<ColumnDefinition Width="*" />
46+
<ColumnDefinition Width="*"/>
4547
</Grid.ColumnDefinitions>
4648
<StackPanel Grid.Column="0">
4749
<GroupBox Header="Hardware Information">
4850
<Grid>
4951
<Grid.ColumnDefinitions>
50-
<ColumnDefinition Width="110"/>
51-
<ColumnDefinition Width="270"/>
52+
<ColumnDefinition Width="1*"/>
53+
<ColumnDefinition Width="2*" MinWidth="200"/>
5254
</Grid.ColumnDefinitions>
5355
<Grid.RowDefinitions>
5456
<RowDefinition />
@@ -57,22 +59,22 @@
5759
<RowDefinition />
5860
</Grid.RowDefinitions>
5961
<Label Grid.Row="0" Grid.Column="0">BIOS Manufacturer</Label>
60-
<TextBox IsReadOnly="True" x:Name="BIOS_Manufacturer" Grid.Row="0" Grid.Column="1" Background="#FFECECEC"></TextBox>
62+
<TextBox IsReadOnly="True" x:Name="BIOS_Manufacturer" Grid.Row="0" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
6163
<Label Grid.Row="1" Grid.Column="0">BIOS Name</Label>
62-
<TextBox IsReadOnly="True" x:Name="BIOS_Name" Grid.Row="1" Grid.Column="1" Background="#FFECECEC"></TextBox>
64+
<TextBox IsReadOnly="True" x:Name="BIOS_Name" Grid.Row="1" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
6365
<Label Grid.Row="2" Grid.Column="0">BIOS S/N</Label>
64-
<TextBox IsReadOnly="True" x:Name="BIOS_SN" Grid.Row="2" Grid.Column="1" Background="#FFECECEC"></TextBox>
66+
<TextBox IsReadOnly="True" x:Name="BIOS_SN" Grid.Row="2" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
6567
<Label Grid.Row="3" Grid.Column="0">BIOS Version</Label>
66-
<TextBox IsReadOnly="True" x:Name="BIOS_Version" Grid.Row="3" Grid.Column="1" Background="#FFECECEC"></TextBox>
68+
<TextBox IsReadOnly="True" x:Name="BIOS_Version" Grid.Row="3" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
6769
</Grid>
6870
</GroupBox>
6971
</StackPanel>
7072
<StackPanel Grid.Column="1">
7173
<GroupBox Header="Operating System Information">
7274
<Grid>
7375
<Grid.ColumnDefinitions>
74-
<ColumnDefinition Width="130"/>
75-
<ColumnDefinition Width="250"/>
76+
<ColumnDefinition Width="1*"/>
77+
<ColumnDefinition Width="2*" MinWidth="200"/>
7678
</Grid.ColumnDefinitions>
7779
<Grid.RowDefinitions>
7880
<RowDefinition/>
@@ -81,26 +83,26 @@
8183
<RowDefinition/>
8284
</Grid.RowDefinitions>
8385
<Label Grid.Row="0" Grid.Column="0">Computer Name</Label>
84-
<TextBox IsReadOnly="True" x:Name="Computer_Name" Grid.Row="0" Grid.Column="1" Background="#FFECECEC"></TextBox>
86+
<TextBox IsReadOnly="True" x:Name="Computer_Name" Grid.Row="0" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
8587
<Label Grid.Row="1" Grid.Column="0">Domain/Workgroup</Label>
86-
<TextBox IsReadOnly="True" x:Name="Computer_Domain" Grid.Row="1" Grid.Column="1" Background="#FFECECEC"></TextBox>
88+
<TextBox IsReadOnly="True" x:Name="Computer_Domain" Grid.Row="1" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
8789
<Label Grid.Row="2" Grid.Column="0">Physical Memory</Label>
88-
<TextBox IsReadOnly="True" x:Name="Computer_Memory" Grid.Row="2" Grid.Column="1" Background="#FFECECEC"></TextBox>
90+
<TextBox IsReadOnly="True" x:Name="Computer_Memory" Grid.Row="2" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
8991
<Label Grid.Row="3" Grid.Column="0">System Type</Label>
90-
<TextBox IsReadOnly="True" x:Name="Computer_SysType" Grid.Row="3" Grid.Column="1" Background="#FFECECEC"></TextBox>
92+
<TextBox IsReadOnly="True" x:Name="Computer_SysType" Grid.Row="3" Grid.Column="1" Background="#FFECECEC" VerticalAlignment="Center"></TextBox>
9193
</Grid>
9294
</GroupBox>
9395
</StackPanel>
9496
</Grid>
9597
<Grid Grid.Row="2">
9698
<Grid.Margin>
97-
<Thickness Left="10" Right="10"></Thickness>
99+
<Thickness Top="5" Left="5" Right="5" Bottom="5"></Thickness>
98100
</Grid.Margin>
99101
<Grid.ColumnDefinitions>
100-
<ColumnDefinition Width="90"/>
101-
<ColumnDefinition Width="690"/>
102+
<ColumnDefinition Width="Auto"/>
103+
<ColumnDefinition Width="9*"/>
102104
</Grid.ColumnDefinitions>
103-
<TextBlock Name="Bottom_Label" Grid.Column="0" VerticalAlignment="Center">Welcome</TextBlock>
105+
<TextBlock Name="Bottom_Label" Grid.Column="0" VerticalAlignment="Center" Margin="10,0,10,0">Welcome</TextBlock>
104106
<ProgressBar Name="Bottom_ProgressBar" Grid.Column="1" Height="20" VerticalAlignment="Center"></ProgressBar>
105107
</Grid>
106108
</Grid>

MainWindow.xaml.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
7070
*/
7171
private async void ConnectToAnotherComputer_Button(object sender, RoutedEventArgs e)
7272
{
73-
// Initialize the ConnectForm instance
74-
ConnectForm connectForm = new ConnectForm();
75-
7673
// Get the user credentials from the ConnectForm
77-
(string computerName, string username, SecureString password) userCredential = connectForm.ReturnValue();
74+
(string computerName, string username, SecureString password) userCredential = (new ConnectForm()).ReturnValue();
7875

7976
// If the computer name is not empty, create a new MachineMethods instance
8077
if (userCredential.computerName != "")
@@ -91,7 +88,6 @@ private async void ConnectToAnotherComputer_Button(object sender, RoutedEventArg
9188
*/
9289
private void Exit_Button(object sender, RoutedEventArgs e)
9390
{
94-
// Shut down the application
9591
Application.Current.Shutdown();
9692
}
9793

@@ -200,7 +196,7 @@ private void RegularException_Click(object sender, RoutedEventArgs e)
200196
{
201197
try
202198
{
203-
throw new CommonException("This is a common exception example.");
199+
throw new ManagementException("Unable to manage (example exception handler).");
204200
}
205201
catch (Exception ex)
206202
{
@@ -216,7 +212,7 @@ private void CriticalException_Click(object sender, RoutedEventArgs e)
216212
{
217213
try
218214
{
219-
throw new CriticalException("This is a critical exception example.");
215+
throw new Exception("Critical exception (example exception handler).");
220216
}
221217
catch (Exception ex)
222218
{

WMIMethods/CommonException.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

WMIMethods/CriticalException.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

WMIMethods/MachineMethods.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,18 @@ public async Task<ManagementObjectCollection> GetObjects(string className, strin
162162
*/
163163
public async Task CallMethod(string className, string fields, string methodName, object[] args)
164164
{
165-
// Create the WMI query
166-
ObjectQuery query = new ObjectQuery($"SELECT {fields} FROM {className}");
167-
// Execute the method asynchronously
168-
await Task.Run(() =>
169-
{
170-
// Create the ManagementObjectSearcher object
171-
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
165+
// Get the WMI objects asynchronously
166+
using (Task<ManagementObjectCollection> manageObject = GetObjects(className, fields)) {
167+
await Task.Run(() =>
172168
{
173169
// Invoke the method on each object returned by the query
174-
foreach (ManagementObject manageObject in searcher.Get().Cast<ManagementObject>())
170+
foreach (ManagementObject obj in manageObject.Result.Cast<ManagementObject>())
175171
{
176172
// Invoke the specified method with arguments
177-
_ = manageObject.InvokeMethod(methodName, args);
173+
_ = obj.InvokeMethod(methodName, args);
178174
}
179-
}
180-
});
175+
});
176+
}
181177
}
182178
}
183179
}

0 commit comments

Comments
 (0)