我需要为我的应用程序打开特定端口。
我已尝试对所有端口的每个应用程序使用 INetFwAuthorizedApplication
规则。
fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app)
或者使用 INetFwOpenPort
为所有应用程序打开一个端口。
firewallManager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port)
有没有办法以编程方式以编程方式仅打开每个应用程序的单个端口? 我可以通过防火墙设置手动完成。
最佳答案
有一个关于阻止连接的问题,答案包含在 C# 中创建防火墙规则的说明。您应该能够根据我想象的任何类型的防火墙规则调整它。
https://stackoverflow.com/a/1243026/12744
The following code creates a firewall rule that blocks any outgoing connections on all of your network adapters:
using NetFwTypeLib; // Located in FirewallAPI.dll ... INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FWRule")); firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; firewallRule.Description = "Used to block all internet access."; firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.Name = "Block Internet"; INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); firewallPolicy.Rules.Add(firewallRule);
关于c# - 创建防火墙规则以在 C# 中以编程方式打开每个应用程序的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8488227/