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

Removal on path to file instead of a rule name #28

Closed Agagamand closed 5 years ago

Agagamand commented 5 years ago

I only know path to.exe, I do not know the name of Firewoll rule. How can I find a rule (to remove it) knowing only the path to the file?

falahati commented 5 years ago

Win Vista+:

var rule = FirewallWAS.Instance.Rules
                     .FirstOrDefault(r => r.ApplicationName == myAppname);
if (rule != null) {
    FirewallWAS.Instance.Rules.Remove(rule);
}

Win XP:

var rule = FirewallLegacy.Instance.Rules
                     .OfType<FirewallLegacyApplicationRule>()
                     .FirstOrDefault(r => r.ExecutableAddress == myAppname);
if (rule != null) {
    FirewallLegacy.Instance.Rules.Remove(rule);
}

This is for current v2 alpha. Doing the same with 1.6 is also quite similar to this; just the name of some of the classes should be replaced.

Agagamand commented 5 years ago

FirewallWAS in 1.6 its FirewallManager, but I can not find analog ApplicationName in version 1.6.

falahati commented 5 years ago

FirewallWAS in 1.6 is FirewallAPIv2.Firewall

var rule = WindowsFirewallHelper.FirewallAPIv2.Firewall.Instance.Rules
                     .OfType<WindowsFirewallHelper.FirewallAPIv2.Rules.StandardRule>()
                     .FirstOrDefault(r => r.ApplicationName == myAppname);
if (rule != null) {
    WindowsFirewallHelper.FirewallAPIv2.Firewall.Instance.Rules.Remove(rule);
}
Agagamand commented 5 years ago

VisualStudeo writes an error ".OfType": «method which is not valid in the given context error»

You have not updated NuGet package WindowsFirewallHelper v2 alpha for a long time

falahati commented 5 years ago

For OfType() you need to import System.Linq. I suggest using tools like ReSharper. These tools will help you when you are missing an import or other similar things. This speeds up your development.

I also forgot to write the () after OfType so the error message might be due to that as well. Comments edited to fix that. These things happen when you try to write a piece of code from memory and not using an IDE. xD