Can I get the firewall's status by c# code?
Can I get the firewall's status by c# code? I want to inform the user when his firewall 开发者_JAVA百科blocked
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
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".
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.
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;
}
精彩评论