In this article, we use com references, because the operations are too complicated without com references.
1. Add com references
In reference, select the com page and findNetfwtypelib, OK
II. Introduction of namespaces
UsingNetfwtypelib;
3. Add exceptions that allow access to the firewallProgram
// Create a firewall management instanceInetfwmgr netfwmgr = (inetfwmgr) activator. createinstance (type. gettypefromprogid ("Hnetcfg. fwmgr"));// Create an instance of the authentication program classInetfwauthorizedapplication APP = (inetfwauthorizedapplication) activator. createinstance (type. gettypefromprogid ("Hnetcfg. fwauthorizedapplication"));// The name displayed by the program in the exception listApp. Name ="Custom";// The program determines the path. Here the program itself is used.App. processimagefilename = application. executablepath;// Whether to enable this ruleApp. Enabled =True;// Add the management policy to the firewallNetfwmgr. localpolicy. currentprofile. authorizedapplications. Add (APP );
4. delete a program in the exception list
inetfwmgr netfwmgr = (inetfwmgr) activator. createinstance (type. gettypefromprogid (" hnetcfg. fwmgr "); // the absolute path of the Program netfwmgr. localpolicy. currentprofile. authorizedapplications. remove (application. executablepath);