1- using System ;
1+ using GetWMIBasic . DefaultUI ;
2+ using GetWMIBasic . WMIMethods ;
3+ using System ;
24using System . Linq ;
35using System . Management ;
4- using System . Runtime . InteropServices ;
56using System . Security ;
67using System . Threading . Tasks ;
78using System . Windows ;
89
9- using GetWMIBasic . WMIMethods ;
10-
1110namespace GetWMIBasic
1211{
1312 public partial class MainWindow : Window
@@ -41,14 +40,7 @@ private async void ConnectToAnotherComputer_Button(object sender, RoutedEventArg
4140 if ( userCredential . computerName != "" )
4241 {
4342 machine = null ;
44- if ( ! userCredential . computerName . Equals ( "localhost" ) )
45- {
46- machine = new MachineMethods ( userCredential ) ;
47- }
48- else
49- {
50- machine = new MachineMethods ( ) ;
51- }
43+ machine = ! userCredential . computerName . Equals ( "localhost" ) ? new MachineMethods ( userCredential ) : new MachineMethods ( ) ;
5244
5345 await Refresh ( ) ;
5446 }
@@ -75,30 +67,9 @@ private async void Restart_Button(object sender, RoutedEventArgs e)
7567 machine . Connect ( "root\\ cimv2" ) ;
7668 await machine . CallMethod ( "Win32_OperatingSystem" , "*" , "Win32Shutdown" , new object [ ] { 6 } ) ;
7769 }
78- catch ( UnauthorizedAccessException ex )
70+ catch ( Exception ex )
7971 {
80- #if DEBUG
81- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
82- #else
83- MessageBox . Show ( $ "Failed to catch the Authenticate with { machine . GetComputerName ( ) } : { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
84- #endif
85- }
86- catch ( COMException ex )
87- {
88- #if DEBUG
89- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
90- #else
91- MessageBox . Show ( $ "Failed to reach { machine . GetComputerName ( ) } : { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
92- #endif
93- }
94-
95- catch ( ManagementException ex )
96- {
97- #if DEBUG
98- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
99- #else
100- MessageBox . Show ( $ "Failed to catch the Management Method: { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
101- #endif
72+ exView . HandleException ( ex ) ;
10273 }
10374 finally
10475 {
@@ -123,30 +94,9 @@ private async void Shutdown_Button(object sender, RoutedEventArgs e)
12394 machine . Connect ( "root\\ cimv2" ) ;
12495 await machine . CallMethod ( "Win32_OperatingSystem" , "*" , "Win32Shutdown" , new object [ ] { 5 } ) ;
12596 }
126- catch ( UnauthorizedAccessException ex )
97+ catch ( Exception ex )
12798 {
128- #if DEBUG
129- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
130- #else
131- MessageBox . Show ( $ "Failed to catch the Authenticate with { machine . GetComputerName ( ) } : { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
132- #endif
133- }
134- catch ( COMException ex )
135- {
136- #if DEBUG
137- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
138- #else
139- MessageBox . Show ( $ "Failed to reach { machine . GetComputerName ( ) } : { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
140- #endif
141- }
142-
143- catch ( ManagementException ex )
144- {
145- #if DEBUG
146- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
147- #else
148- MessageBox . Show ( $ "Failed to catch the Management Method: { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
149- #endif
99+ ( new ExceptionView ( ) ) . HandleException ( ex ) ;
150100 }
151101 finally
152102 {
@@ -160,6 +110,30 @@ private async void Refresh_Button(object sender, RoutedEventArgs e)
160110 await Refresh ( ) ;
161111 }
162112
113+ private void RegularException_Click ( object sender , RoutedEventArgs e )
114+ {
115+ try
116+ {
117+ throw new CommonException ( "This is a common exception example." ) ;
118+ }
119+ catch ( Exception ex )
120+ {
121+ exView . HandleException ( ex ) ;
122+ }
123+ }
124+
125+ private void CriticalException_Click ( object sender , RoutedEventArgs e )
126+ {
127+ try
128+ {
129+ throw new CriticalException ( "This is a critical exception example." ) ;
130+ }
131+ catch ( Exception ex )
132+ {
133+ exView . HandleException ( ex ) ;
134+ }
135+ }
136+
163137 /*
164138 * Non-button methods
165139 */
@@ -171,7 +145,7 @@ private async Task Refresh()
171145 {
172146 machine . Connect ( "root\\ cimv2" ) ;
173147
174- var biosProperties = machine . GetObjects ( "Win32_BIOS" , "* " ) ;
148+ Task < ManagementObjectCollection > biosProperties = machine . GetObjects ( "Win32_BIOS" , "Manufacturer, Name, SerialNumber, Version " ) ;
175149 foreach ( ManagementObject biosProperty in ( await biosProperties ) . Cast < ManagementObject > ( ) )
176150 {
177151 BIOS_Manufacturer . Text = biosProperty [ "Manufacturer" ] ? . ToString ( ) ?? string . Empty ;
@@ -180,7 +154,7 @@ private async Task Refresh()
180154 BIOS_Version . Text = biosProperty [ "Version" ] ? . ToString ( ) ?? string . Empty ;
181155 }
182156
183- var osProperties = machine . GetObjects ( "Win32_ComputerSystem" , "* " ) ;
157+ Task < ManagementObjectCollection > osProperties = machine . GetObjects ( "Win32_ComputerSystem" , "Name, Domain, TotalPhysicalMemory, SystemType " ) ;
184158 foreach ( ManagementObject osProperty in ( await osProperties ) . Cast < ManagementObject > ( ) )
185159 {
186160 Computer_Name . Text = osProperty [ "Name" ] ? . ToString ( ) ?? string . Empty ;
@@ -189,30 +163,9 @@ private async Task Refresh()
189163 Computer_SysType . Text = osProperty [ "SystemType" ] ? . ToString ( ) ?? string . Empty ;
190164 }
191165 }
192- catch ( UnauthorizedAccessException ex )
193- {
194- #if DEBUG
195- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
196- #else
197- MessageBox . Show ( $ "Failed to catch the Authenticate with { machine . GetComputerName ( ) } : { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
198- #endif
199- }
200- catch ( COMException ex )
201- {
202- #if DEBUG
203- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
204- #else
205- MessageBox . Show ( $ "Failed to reach { machine . GetComputerName ( ) } : { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
206- #endif
207- }
208-
209- catch ( ManagementException ex )
166+ catch ( Exception ex )
210167 {
211- #if DEBUG
212- MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
213- #else
214- MessageBox . Show ( $ "Failed to catch the Management Method: { ex . Message } ", "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
215- #endif
168+ exView . HandleException ( ex ) ;
216169 }
217170 finally
218171 {
0 commit comments