11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using System . ServiceProcess ;
54using WindowsFirewallHelper . COMInterop ;
65using WindowsFirewallHelper . FirewallAPIv2 . Rules ;
76using WindowsFirewallHelper . Helpers ;
@@ -34,18 +33,6 @@ public Firewall()
3433 } ;
3534 }
3635
37- public IEnumerable < FirewallRuleGroup > RuleGroups
38- {
39- get
40- {
41- return Rules
42- . Select ( rule => rule . Grouping )
43- . Where ( s => ! string . IsNullOrWhiteSpace ( s ) )
44- . Distinct ( )
45- . Select ( s => new FirewallRuleGroup ( this , s ) ) ;
46- }
47- }
48-
4936 /// <summary>
5037 /// Gets the current singleton instance of this class
5138 /// </summary>
@@ -54,6 +41,14 @@ public static Firewall Instance
5441 get => GetInstance ( ) ;
5542 }
5643
44+ /// <summary>
45+ /// Gets a Boolean value showing if the firewall is supported in this environment.
46+ /// </summary>
47+ public static bool IsSupported
48+ {
49+ get => Type . GetTypeFromProgID ( @"HNetCfg.FwPolicy2" , false ) != null ;
50+ }
51+
5752 /// <summary>
5853 /// Gets a value indicating if adding or setting a rule or group of rules will take effect in the current firewall
5954 /// profile
@@ -70,9 +65,22 @@ public FirewallModifyStatePolicy LocalModifyStatePolicy
7065 return ( FirewallModifyStatePolicy ) UnderlyingObject . LocalPolicyModifyState ;
7166 }
7267 }
73- public static bool IsServiceRunning
68+
69+ public IEnumerable < FirewallRuleGroup > RuleGroups
7470 {
75- get => new ServiceController ( "MpsSvc" ) . Status == ServiceControllerStatus . Running ;
71+ get
72+ {
73+ return Rules
74+ . Select ( rule => rule . Grouping )
75+ . Where ( s => ! string . IsNullOrWhiteSpace ( s ) )
76+ . Distinct ( )
77+ . Select ( s => new FirewallRuleGroup ( this , s ) ) ;
78+ }
79+ }
80+
81+ public ICollection < StandardRule > Rules
82+ {
83+ get => new FirewallRulesCollection < StandardRule > ( UnderlyingObject . Rules ) ;
7684 }
7785
7886 internal INetFwPolicy2 UnderlyingObject { get ; }
@@ -249,14 +257,13 @@ public IRule CreatePortRule(FirewallProfiles profiles, string name, ushort portN
249257
250258 /// <inheritdoc />
251259 /// <summary>
252- /// Returns a specific firewall profile
260+ /// Returns the active firewall profile, if any
253261 /// </summary>
254- /// <param name="profile">Requested firewall profile</param>
255- /// <returns>Firewall profile object implementing <see cref="IProfile" /> interface</returns>
256- /// <exception cref="NotSupportedException">This class is not supported on this machine</exception>
257- /// <exception cref="FirewallAPIv2NotSupportedException">The asked profile is not supported with this class</exception>
258- // ReSharper disable once FlagArgument
259- public IProfile GetProfile ( FirewallProfiles profile )
262+ /// <returns>
263+ /// The active firewall profile object implementing <see cref="IProfile" /> interface or null if no firewall
264+ /// profile is currently active
265+ /// </returns>
266+ public IProfile GetActiveProfile ( )
260267 {
261268 if ( ! IsSupported )
262269 {
@@ -265,24 +272,25 @@ public IProfile GetProfile(FirewallProfiles profile)
265272
266273 foreach ( var p in Profiles )
267274 {
268- if ( p . Type == profile )
275+ if ( p . IsActive )
269276 {
270277 return p ;
271278 }
272279 }
273280
274- throw new FirewallAPIv2NotSupportedException ( ) ;
281+ return null ;
275282 }
276283
277284 /// <inheritdoc />
278285 /// <summary>
279- /// Returns the active firewall profile, if any
286+ /// Returns a specific firewall profile
280287 /// </summary>
281- /// <returns>
282- /// The active firewall profile object implementing <see cref="IProfile" /> interface or null if no firewall
283- /// profile is currently active
284- /// </returns>
285- public IProfile GetActiveProfile ( )
288+ /// <param name="profile">Requested firewall profile</param>
289+ /// <returns>Firewall profile object implementing <see cref="IProfile" /> interface</returns>
290+ /// <exception cref="NotSupportedException">This class is not supported on this machine</exception>
291+ /// <exception cref="FirewallAPIv2NotSupportedException">The asked profile is not supported with this class</exception>
292+ // ReSharper disable once FlagArgument
293+ public IProfile GetProfile ( FirewallProfiles profile )
286294 {
287295 if ( ! IsSupported )
288296 {
@@ -291,21 +299,13 @@ public IProfile GetActiveProfile()
291299
292300 foreach ( var p in Profiles )
293301 {
294- if ( p . IsActive )
302+ if ( p . Type == profile )
295303 {
296304 return p ;
297305 }
298306 }
299307
300- return null ;
301- }
302-
303- /// <summary>
304- /// Gets a Boolean value showing if the firewall is supported in this environment.
305- /// </summary>
306- public static bool IsSupported
307- {
308- get => Type . GetTypeFromProgID ( @"HNetCfg.FwPolicy2" , false ) != null ;
308+ throw new FirewallAPIv2NotSupportedException ( ) ;
309309 }
310310
311311 /// <inheritdoc />
@@ -326,18 +326,7 @@ public string Name
326326 /// <inheritdoc />
327327 ICollection < IRule > IFirewall . Rules
328328 {
329- get
330- {
331- return new FirewallRulesCollection < IRule > ( UnderlyingObject . Rules ) ;
332- }
333- }
334-
335- public ICollection < StandardRule > Rules
336- {
337- get
338- {
339- return new FirewallRulesCollection < StandardRule > ( UnderlyingObject . Rules ) ;
340- }
329+ get => new FirewallRulesCollection < IRule > ( UnderlyingObject . Rules ) ;
341330 }
342331
343332 /// <summary>
0 commit comments