C # control system firewall

Source: Internet
Author: User

Using system; using system. collections. generic; using system. text; using netfwtypelib; namespace firewalltest {public class firewallhelp {///// Add the firewall exception port ///// name /// port /// protocol (TCP and UDP) public static void netfwaddports (string name, int port, string Protocol)
{// Create an instance of the firewall management class, inetfwmgr netfwmgr = (inetfwmgr) activator. createinstance (type. gettypefromprogid ("hnetcfg. fwmgr "); inetfwopenport objport = (inetfwopenport) activator. createinstance (type. gettypefromprogid ("hnetcfg. fwopenport "); objport. name = Name; objport. port
= Port; If (protocol. toupper () = "TCP") {objport. protocol = net_fw_ip_protocol_.net_fw_ip_protocol_tcp;} else {objport. protocol = net_fw_ip_protocol_.net_fw_ip_protocol_udp;} objport. scope = net_fw_scope_.net_fw_scope_all; objport. enabled = true; bool
Exist = false; // Add the Management Policy foreach (inetfwopenport mport in netfwmgr. localpolicy. currentprofile. globallyopenports) {If (objport = mport) {exist = true; break ;}} if (exist) {system. windows. forms. messageBox. show ("exist");} If (! Exist) netfwmgr. localpolicy. currentprofile. globallyopenports. Add (objport );
} // Add the application to the firewall exception // Application name // the full path of the application executable file public static void netfwaddapps (string name, string executablepath) {// create the firewall management instance inetfwmgr netfwmgr = (inetfwmgr) activator. createinstance (type. gettypefromprogid ("hnetcfg. fwmgr "); inetfwauthorizedapplication
APP = (inetfwauthorizedapplication) activator. createinstance (type. gettypefromprogid ("hnetcfg. fwauthorizedapplication "); // In the exception list, the name displayed by the program is app. name = Name; // program path and file name app. processimagefilename = executablepath; // whether to enable the rule app. enabled = true; // The Management Policy netfwmgr added to the firewall. localpolicy. currentprofile. authorizedapplications. add (APP );
Bool exist = false; // Add the Management Policy foreach (inetfwauthorizedapplication Mapp in netfwmgr. localpolicy. currentprofile. authorizedapplications) {If (APP = MAPP) {exist = true; break ;}} if (! Exist) netfwmgr. localpolicy. currentprofile. authorizedapplications. Add (APP );
} ///// Delete the firewall exception port ///// port // protocol (TCP, UDP) public static void netfwdelapps (INT port, string Protocol) {inetfwmgr netfwmgr = (inetfwmgr) activator. createinstance (type. gettypefromprogid ("hnetcfg. fwmgr "); If (Protocol =" TCP ") {netfwmgr. localpolicy. currentprofile. globallyopenports. remove (port,
Net_fw_ip_protocol_.net_fw_ip_protocol_tcp);} else {netfwmgr. localpolicy. currentprofile. globallyopenports. remove (port, net_fw_ip_protocol_.net_fw_ip_protocol_udp) ;}///// Delete the absolute path of the application program in the firewall exceptions ///// public static void netfwdelapps (string executablepath)
{Inetfwmgr netfwmgr = (inetfwmgr) activator. createinstance (type. Assign ("hnetcfg. fwmgr"); netfwmgr. localpolicy. currentprofile. authorizedapplications. Remove (executablepath );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.