Can I get the firewall's status by c# code? I want to inform the user when his firewall blocked
-
7Do you mean Windows' built-in Firewall, or just any firewall? Can you be more specific? – Vivelin Jul 05 '10 at 12:29
-
@horsedrowner: any Windows Firewall has to export its status trough WMI, otherwise the Security Center would show the "red shield". – lornova Jul 05 '10 at 12:51
-
something similar [Check for Third Party Firewalls on a Machine](http://stackoverflow.com/questions/13615203/check-for-third-party-firewalls-on-a-machine) – Microsoft DN Oct 09 '13 at 13:38
4 Answers
You can use the following links to interact with Windows firewall. Include NetFwTypeLib as a reference to your project.
For Window Firewall you can create the manager with the following code:
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr manager= (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
from there you can read about the various methods to configure with windows firewall.
Windows Firewall (Windows XP...limited Vista and 7 support) http://msdn.microsoft.com/en-us/library/aa366452(v=VS.85).aspx
Windows Firewall with advanced security (Windows Vista/7)
msdn.microsoft.com/en-us/library/aa366459(v=VS.85).aspx

- 471
- 6
- 16
On a Windows 7 machine this is what I do in order to detect whether or not the machine has the Windows Firewall enabled. I take the same approach as Dylan suggested.
Just remember to add a reference to Microsoft.TeamFoundation.Common
.
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
bool firewallEnabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
I would like to see a WMI solution of this task, but this has worked well for me.

- 9,708
- 5
- 58
- 67
Assuming you have 3 labels, this will show the status of all of the firewalls in a form Application.
using System.Diagnostics;
private string getFirewallStatus()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);
if (process != null)
{
process.StandardInput.WriteLine("netsh advfirewall show allprofiles | find \"State\"");
process.StandardInput.Close();
string outputString = process.StandardOutput.ReadToEnd();
int count = 0;
for (int i = 0; i < outputString.Length - 3; i++)
{
if (outputString.Substring(i, 3).CompareTo(@"OFF") == 0)
{
count++;
switch (count)
{
case 1: label16.Text = "Off"; label16.ForeColor = System.Drawing.Color.Green; break;
case 2: label17.Text = "Off"; label17.ForeColor = System.Drawing.Color.Green; break;
case 3: label18.Text = "Off"; label18.ForeColor = System.Drawing.Color.Green; break;
default: MessageBox.Show("Firewall status unable to be found!"); break;
}
}
else if (outputString.Substring(i, 2).CompareTo("ON") == 0)
{
count++;
}
}
count = 0;
return outputString;
}
return string.Empty;
}

- 19
- 1
- 6
Could use Windows Security Center APIs. https://learn.microsoft.com/en-us/windows/win32/api/wscapi/
[DllImport("wscapi.dll")]
private static extern int WscGetSecurityProviderHealth(int inValue, ref int outValue);
var outValue = -1;
var commandResultCode = WscGetSecurityProviderHealth(1, ref outValue);
if (commandResultCode > 0)
{
return outValue == 0;
}

- 2,816
- 20
- 37