falahati / WindowsFirewallHelper

A class library to manage the Windows Firewall as well as adding your program to the Windows Firewall Exception list.
MIT License
276 stars 72 forks source link

UnauthorizedAccessException when adding a new rule #61

Closed Slinky67 closed 1 year ago

Slinky67 commented 1 year ago

This is my first approach to this library and not sure if I'm missing something else or it's a bug. I get an UnauthorizedAccessException when adding a new rule. I'm using version 2.2.0.86

// Create and add rules for this app try { string ruleRootName = @"Myapp "; FirewallProfiles allProfiles = FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public; string thePath = Assembly.GetExecutingAssembly().Location; var r1 = FirewallManager.Instance.CreateApplicationRule(allProfiles, ruleRootName + @"Rule (Outbound)", FirewallAction.Allow, thePath); r1.Direction = FirewallDirection.Outbound; FirewallManager.Instance.Rules.Add(r1); } catch (UnauthorizedAccessException e) { Utils.LogV("Failed to set firewall rules"); }

Slinky67 commented 1 year ago

It's okay now. It was a problem with missing administrator rights of my app.