Can I get firewall status using C # code?

Can I get firewall status using C # code? I want to inform the user when his firewall is blocked

+4
source share
4 answers

You can do this through WMI, since any firewall must report its status to WMI (this means that the Security Center displays the status).

There is very little information on the Internet, these can be starting points:

http://www.mombu.com/microsoft/windows-xp-wmi/t-remotely-get-wmi-info-from-security-center-601256.html

http://msdn2.microsoft.com/en-us/library/ms950397.aspx

The next step is to access the WMI classes in C #:

http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/

http://geekswithblogs.net/PsychoCoder/archive/2008/01/25/using_wmi_in_csharp.aspx

... and many others, just google "C # WMI".

+3
source

You can use the following links to interact with the Windows firewall. Include NetFwTypeLib as a reference to your project.

For Window Firewall, you can create a manager with the following code:

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 

INetFwMgr manager= (INetFwMgr)Activator.CreateInstance(NetFwMgrType);

from there you can read about various methods for configuring the Windows firewall.

Windows Firewall (Windows XP ... limited support for Vista and 7) 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

+3
source

Assuming you have 3 labels, this will show the status of all firewalls in the 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; } 
0
source

On a Windows 7 machine, this is what I am doing to determine if Windows Firewall is enabled. I take the same approach as Dylan suggested.

Remember to add a link 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 to this problem, but it worked well for me.

0
source

Source: https://habr.com/ru/post/1314731/


All Articles