Wednesday, June 6, 2007

Enable / Disable Firewall Programatically using VBScript/C#

'VbScript Code to enable firewall
objFirewall = CreateObject("HNetCfg.FwMgr")
Set objPolicy = objFirewall.LocalPolicy.CurrentProfile
objPolicy.FirewallEnabled = TRUE

'VbScript Code to enable firewall
objFirewall = CreateObject("HNetCfg.FwMgr")
Set objPolicy = objFirewall.LocalPolicy.CurrentProfile
objPolicy.FirewallEnabled = FALSE

'You may want to call the above code using C# - The below code just try to make a vbs file at runtime using C# and calls it.


public void enableFirewall(){
try {
string filename=Application.StartupPath + "\\" + "firewall.vbs";
string enableFirewallScript="Set objFirewall = CreateObject(\"HNetCfg.FwMgr\") ";
enableFirewallScript=enableFirewallScript + "\n" + "Set objPolicy=objFirewall.LocalPolicy.CurrentProfile" + "\n";
enableFirewallScript=enableFirewallScript + "objPolicy.FirewallEnabled = TRUE" + "\n";
enableFirewallScript=enableFirewallScript + "MsgBox(\"Firewall Enabled\")";
CreateVBS("firewall.vbs",enableFirewallScript);try {
System.Diagnostics.Process.Start(@filename);
}
catch {
MessageBox.Show("Can't access Firewall","Alert!",MessageBoxButtons.OK,MessageBoxIcon.Hand);
}}
catch {}
}

1 comment:

Anonymous said...

PLS GIVE THE VBscript code to "Enable" Or "Disable" Windows XP firewall on local network Computer..
pls. help.
From Dhimant, A'bad...
thanks