public sealed class WindowsFirewallClient Local Windows Firewall administration using generated native bindings and the caller's privileges.
Remarks
Writes go to the local policy store. By default they are refused while WindowsFirewallClient.GetLocalPolicyStateAsync is not FirewallLocalPolicyState.Effective; see FirewallClientOptions.WriteWhenLocalPolicyOverridden.
Constructors
WindowsFirewallClient
public WindowsFirewallClient() Creates a local firewall client.
WindowsFirewallClient
public WindowsFirewallClient(FirewallClientOptions options) Creates a local firewall client with explicit write behavior.
Methods
CreateAsync
public Task CreateAsync(FirewallRuleSpecification specification, CancellationToken cancellationToken = default) Creates a rule; an existing native name is a conflict.
Remarks
An inbound allow rule needs a program, service, local port or ICMP protocol; otherwise this throws ArgumentException.
DeleteAllAsync
public Task<int> DeleteAllAsync(string name, CancellationToken cancellationToken = default) Deletes every rule whose native name matches name without regard to case, as netsh advfirewall firewall delete rule name=… does, and returns how many were deleted (zero if none).
Remarks
Each matching rule is renamed to a unique temporary name and removed by that name, so a rule that takes the name concurrently is not removed. Deletion is not transactional: if a removal fails, the rules removed before it stay removed.
DeleteAsync
public Task<bool> DeleteAsync(FirewallRuleIdentity identity, CancellationToken cancellationToken = default) Deletes a uniquely identified rule. Returns false only if absent.
Remarks
The rule that matched identity is renamed to a unique temporary name and removed by that name, so a rule that takes the name concurrently is not removed.
EnumerateAsync
public Task<ImmutableArray<FirewallRuleSnapshot>> EnumerateAsync(CancellationToken cancellationToken = default) Enumerates rules without treating duplicate native names as distinct persistent IDs.
FindAsync
public Task<FirewallRuleSnapshot?> FindAsync(string name, CancellationToken cancellationToken = default) Finds a unique rule by native name. Ambiguous names fail.
GetLocalPolicyStateAsync
public Task<FirewallLocalPolicyState> GetLocalPolicyStateAsync(CancellationToken cancellationToken = default) Reads whether rules written to the local store take effect under the current profiles (INetFwPolicy2.LocalPolicyModifyState).
GetProfilesAsync
public Task<ImmutableArray<FirewallProfileSnapshot>> GetProfilesAsync(CancellationToken cancellationToken = default) Inspects effective profiles without changing global firewall settings.
UpdateAsync
public Task UpdateAsync(FirewallRuleIdentity identity, FirewallRuleUpdate update, CancellationToken cancellationToken = default) Updates selected properties, preserving other native settings. Changes are not transactional.
Remarks
An update that would turn a rule into an inbound allow rule without a program, service, local port or ICMP protocol throws ArgumentException and changes nothing. A rule that already allows such traffic can still be updated.