Window firewall On and off functionality.


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. 1)first add Refrence of the "hnetcfg.dll" avaialable in the
  2. "C:\Windows\System32"
  3.  
  4. 2)Then add these references on the code behind
  5.  
  6. using NATUPNPLib;
  7. using NETCONLib;
  8. using NetFwTypeLib;
  9.  
  10. //////////////////////////////////////////////////////
  11.  
  12. 3)add these methoods
  13.  
  14. private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}";
  15.  
  16. private static NetFwTypeLib.INetFwMgr GetFirewallManager()
  17. {
  18. Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER));
  19. return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr;
  20. }
  21.  
  22. //////////////////////////////////////////////////////
  23. 4)Calling above methood to invoke it on button click.
  24.  
  25. protected void btnDetectFirewal_Click(object sender, EventArgs e)
  26. {
  27. INetFwMgr manager = GetFirewallManager();
  28. bool isFirewallEnabled = manager.LocalPolicy.CurrentProfile.FirewallEnabled;
  29.  
  30. if (isFirewallEnabled == true)
  31. {
  32. manager.LocalPolicy.CurrentProfile.FirewallEnabled = false;
  33. Label1.Text = "Firewall disabled successfully..!";
  34. }
  35. else
  36. {
  37. manager.LocalPolicy.CurrentProfile.FirewallEnabled = true;
  38. Label1.Text = "Firewall enabled successfully..!";
  39. }
  40.  
  41.  
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.