コード例 #1
0
        private static void FirewallActions(ToastArguments args, string fileName, string ruleNameComponent)
        {
            var           buttonSelection = args.First().Value;
            INetFwRule2   firewallRule    = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
            INetFwPolicy2 operatePolicy   = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            var           existRule       = filteredRules.Any(x => x.Name.EndsWith(ruleNameComponent + " 出站连接"));

            if (existRule == false)
            {
                if (buttonSelection == "AllowConnection")
                {
                    var currentProfiles = fwPolicy2.CurrentProfileTypes;
                    firewallRule.Enabled         = true;
                    firewallRule.Direction       = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
                    firewallRule.Action          = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
                    firewallRule.ApplicationName = fileName;
                    firewallRule.Name            = "允许 " + ruleNameComponent + " 出站连接";
                    firewallRule.Profiles        = currentProfiles;
                    operatePolicy.Rules.Add(firewallRule);
                }
                else
                {
                    var currentProfiles = fwPolicy2.CurrentProfileTypes;
                    firewallRule.Enabled         = true;
                    firewallRule.Direction       = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
                    firewallRule.Action          = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
                    firewallRule.ApplicationName = fileName;
                    firewallRule.Name            = "阻止 " + ruleNameComponent + " 出站连接";
                    firewallRule.Profiles        = currentProfiles;
                    operatePolicy.Rules.Add(firewallRule);
                }
            }
            ReadAllFirewallRules();
            displayNotification = true;
            ToastNotificationManagerCompat.History.Clear();
        }