@@ -35,11 +35,11 @@ public static void grant_firewall_rules()
3535
3636 INetFwPolicy2 firewallPolicy = ( INetFwPolicy2 ) Activator . CreateInstance ( Type . GetTypeFromProgID ( "HNetCfg.FwPolicy2" ) ) ;
3737
38- INetFwRule firewallRules = firewallPolicy . Rules . OfType < INetFwRule > ( ) . Where ( x => ( x . Name == "ClabSignals" ) &&
39- ( x . Name == "ClabMessages" ) &&
40- ( x . Name == "ClabFiles" ) ) . FirstOrDefault ( ) ;
38+ bool firewallRules = firewallPolicy . Rules . OfType < INetFwRule > ( ) . Where ( x => ( x . Name == "ClabSignals" ) ||
39+ ( x . Name == "ClabMessages" ) ||
40+ ( x . Name == "ClabFiles" ) ) . Count ( ) == 3 ;
4141
42- if ( firewallRules == null )
42+ if ( ! firewallRules )
4343 {
4444 Common . run_as_admin ( ) ;
4545
@@ -95,32 +95,41 @@ public static partial class Common
9595 {
9696 public static bool is_admin ( )
9797 {
98- WindowsIdentity id = WindowsIdentity . GetCurrent ( ) ;
99- WindowsPrincipal principal = new WindowsPrincipal ( id ) ;
98+ bool admin ;
10099
101- return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
100+ try
101+ {
102+ WindowsIdentity id = WindowsIdentity . GetCurrent ( ) ;
103+ WindowsPrincipal principal = new WindowsPrincipal ( id ) ;
104+ admin = principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
105+ }
106+ catch ( Exception )
107+ {
108+ admin = false ;
109+ }
110+
111+ return admin ;
102112 }
103113
104114 public static void run_as_admin ( )
105115 {
106116 if ( ! is_admin ( ) )
107117 {
108- ProcessStartInfo proc = new ProcessStartInfo ( ) ;
109- proc . UseShellExecute = false ;
110- proc . WorkingDirectory = Environment . CurrentDirectory ;
111- proc . FileName = Assembly . GetEntryAssembly ( ) . CodeBase ;
112- proc . Verb = "runas" ;
118+ Process proc = new Process ( ) ;
119+ proc . StartInfo . Verb = "runas" ;
120+ proc . StartInfo . UseShellExecute = true ;
121+ proc . StartInfo . FileName = Path . ChangeExtension ( Assembly . GetEntryAssembly ( ) . Location , ".exe" ) ;
113122
114123 try
115124 {
116- Process . Start ( proc ) ;
125+ proc . Start ( ) ;
117126 }
118127 catch ( Exception )
119128 {
120- MessageBox . Show ( "Please run Clab as administrator this time. This is required to configure the firewall" ) ;
129+ MessageBox . Show ( "Please run Clab as administrator next time. This is required to configure the firewall" ) ;
121130 }
122131
123- Application . Exit ( ) ;
132+ Environment . Exit ( 0 ) ;
124133 }
125134 }
126135
0 commit comments