Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the firewall's status by c# code?

Tags:

c#

Can I get the firewall's status by c# code? I want to inform the user when his firewall blocked

like image 894
user383659 Avatar asked Jul 05 '10 12:07

user383659


People also ask

How do I check firewall status PowerShell?

To get the current status of Windows Firewall using PowerShell, just type Get-NetFirewallProfile in the PowerShell window and press Enter. You'll be shown a list of all the network profiles, whether Windows Firewall is enabled for each profile and information about various other Windows Firewall settings.


2 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

like image 130
Dylan Avatar answered Oct 20 '22 02:10

Dylan


You can to it via WMI, as any firewall has to report trough WMI its status (it is the way the Security Center shows status).

There's very little information in Internet, these may 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 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".

like image 26
lornova Avatar answered Oct 20 '22 02:10

lornova