Article turned from: http://sdfiyon.iteye.com/blog/1197511
One, add COM references
In the reference, select COM page, find netfwtypelib , OK
Ii. Adding exceptions to allow through the firewall
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingNetFwTypeLib;namespacecommon{ Public Static classInetfwmanger {/// <summary> ///Add firewall exception Port/// </summary> /// <param name= "name" >name</param> /// <param name= "Port" >Port</param> /// <param name= "protocol" >protocol (TCP, UDP)</param> Public Static voidNetfwaddports (stringNameintPortstringprotocol) { //to create an instance of the firewall management classInetfwmgr 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; BOOLexist =false; //Add a management policy to the firewall foreach(INetFwOpenPort mportinchnetFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts) {if(Objport = =mport) {exist=true; Break; } } if(!exist) NetFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add (Objport); } /// <summary> ///add an application to the firewall exception/// </summary> /// <param name= "name" >Application Name</param> /// <param name= "ExecutablePath" >application executable file full path</param> Public Static voidNetfwaddapps (stringNamestringExecutablePath) { //to create an instance of the firewall management classInetfwmgr netfwmgr = (inetfwmgr) activator.createinstance (Type.gettypefromprogid ("Hnetcfg.fwmgr")); INetFwAuthorizedApplication app=(inetfwauthorizedapplication) activator.createinstance (Type.gettypefromprogid ("hnetcfg.fwauthorizedapplication")); //in the Exceptions list, the name of the program displayApp. Name =name; //path and file name of the programApp. Processimagefilename =ExecutablePath; //whether the rule is enabledApp. Enabled =true; //Add a management policy to the firewallnetFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add (APP); BOOLexist =false; //Add a management policy to the firewall foreach(INetFwAuthorizedApplication mAppinchnetFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications) {if(App = =mApp) {exist=true; Break; } } if(!exist) NetFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add (app); } /// <summary> ///Remove firewall exception port/// </summary> /// <param name= "Port" >Port</param> /// <param name= "protocol" >protocol (TCP, UDP)</param> Public Static voidNetfwdelapps (intPortstringprotocol) {inetfwmgr Netfwmgr= (inetfwmgr) activator.createinstance (Type.gettypefromprogid ("Hnetcfg.fwmgr")); if(Protocol = ="TCP") {NetFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove (port, NET_FW_IP_PROTOCOL_.N ET_FW_IP_PROTOCOL_TCP); } Else{netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove (port, Net_fw_ip_protocol_. NET_FW_IP_PROTOCOL_UDP); } } /// <summary> ///Remove the firewall exception in the application/// </summary> /// <param name= "ExecutablePath" >Absolute path of the program</param> Public Static voidNetfwdelapps (stringExecutablePath) {inetfwmgr Netfwmgr= (inetfwmgr) activator.createinstance (Type.gettypefromprogid ("Hnetcfg.fwmgr")); NetFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove (ExecutablePath); } }}
C # Add modify firewall ports and programs