Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to get status of windows firewall. I am successful in doing so. and also able to ON/ Enable the OFF/ Disabled Firewall.

But when i am Disabling the firewall using my code it is showing this error

http://i.stack.imgur.com/hxNHT.jpg[^]

Please tell me how could i make it possible.

Code is here

C#
private void toggleButton1_Checked(object sender, RoutedEventArgs e)
   {
       toggleButton1.Content = "On";
       toggleButton1.Background = Brushes.LawnGreen;
       INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
       mgr.LocalPolicy.CurrentProfile.FirewallEnabled = true;
   }

   private void toggleButton1_Unchecked(object sender, RoutedEventArgs e)
   {
       toggleButton1.Content = "Off";
       toggleButton1.Background = Brushes.Red;
       INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
       mgr.LocalPolicy.CurrentProfile.FirewallEnabled=false;
   }

Thank you
Posted

First I thought that you would only require elevation. I quickly wrote an application to test this theory.
Running the application as normal user, I got the same error. I then restarted my IDE as an administrator and tried again. This time I got the error "Not Yet Implemented". This was using .Net4.....
Why Microsoft says not yet implemented is beyond me.

Question is, why do you need to disable the firewall as this defeats the purpose of the firewall in the first place. You can however add an exception to the firewall.

Here is a piece of my code that I use:

public void OpenPort(string Name, int Port, NET_FW_IP_PROTOCOL_ protocol)
{
       INetFwMgr fwMgr = (INetFwMgr)getInstance("INetFwMgr");
       INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
       INetFwProfile fwProfile = fwPolicy.CurrentProfile;
       INetFwOpenPorts openports = fwProfile.GloballyOpenPorts;
       INetFwOpenPort openport = (INetFwOpenPort)getInstance("INetOpenPort");
       openport.Port = Port;
       //.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP
       openport.Protocol = protocol;
       openport.Name = Name + Port; //The description that will be displayed in windows firewall.
       openports.Add(openport);
       fwMgr = null;
       fwPolicy = null;
       fwProfile = null;
       openports = null;
       openport = null;
   }
 
Share this answer
 
You should be using the Policy2 interop instead of the Firewall Manager interop. That will allow you to set the firewall to enabled equal false.
 
Share this answer
 
Comments
CHill60 14-Dec-15 18:38pm    
Question is over 2 years old!
[no name] 14-Dec-15 20:27pm    
Also, OP is using visual studio 2010 so your solution is wrong anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900