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 );}}}